社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

从类oop python的所有实例创建字典

Ian_De_Oliveira • 6 年前 • 1538 次点击  
class Country(object):
    """ Modeling a country immigration. """

    def __init__(name, immigrants, population, disease_numbers):

        self.name = name
        self.immigrants = immigrants
        self.population = population
        self.disease_numbers = disease_numbers

我有后续课程,这是更多的属性。每年人口都在变化,到了X年年底,我试图建立一本有序的字典,告诉我哪个国家人口最多,疾病最少……我如何建立一个每年/每轮更新的字典,并在最后给我这个信息。

我怎样才能得到最后一回合的数据信息?

我来澄清一下这个问题。

我只需要在模拟的最后有一个有序的字典。

  d = {'self.name' : London
       ('self.immigrants' : 15000
       'self.population' : 500000
       'self.disease_numbers' :3000) , 
        'self.name' : Madrid
       ('self.immigrants' : 17000
       'self.population' : 5000
       'self.disease_numbers' :300)}

然后可以选择例如伦敦,因为有更多的人患有疾病。因此,思考这个问题几乎可以成为一种新的方法,让疾病患者数量更多的城市回归。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38057
 
1538 次点击  
文章 [ 2 ]  |  最新文章 6 年前
Stuart
Reply   •   1 楼
Stuart    6 年前

不完全清楚您打算如何更新国家/地区数据,但听起来您需要的只是将国家/地区数据对象存储在字典中,使用一系列类似数据库的函数进行查询,如下所示:

country_data = {}

def add_country_data(name, year, *args, **kwargs):
    country = Country(name, *args, **kwargs)
    if name in country_data:
        country_data[name][year] = country
    else:
        country_data[name] = {year: country}

def get_latest_data(country_name):
    years = country_data[country_name].keys()
    return country_data[country_name][max(years)]

def get_max_country(attr, year=None):
    """ Returns the county with the max value of the given attribute
    in the given year or (if year is ommitted) any year """
    r = None
    highest = None
    for name, country in country_data.items():
        if year is None:
            max_v = max(getattr(country[y], attr) for y in country.keys())
        else:
            max_v = getattr(country[year], attr)
        if highest is None or max_v > highest:
            highest = max_v
            r = name
    return r, highest

def get_latest_dicts():
    return {name: get_latest_data(name).__dict__ 
            for name in country_data.keys()}

add_country_data("Venezuela", 1989, 100, 20, 50)
add_country_data("Venezuela", 2009, 120, 30, 40)
add_country_data("Brazil", 2008, 110, 40, 90)

print get_latest_data("Venezuela").immigrants   # 120
print get_max_country("infected")     # ('Brazil', 40)
print get_latest_dicts()            # ('Brazil': {'immigrants: 110 ... 

如果需要,可以将这些函数和数据字典作为 class methods

class Country(object):
    """ Modeling a country immigration. """
    data = {}

    def __init__(self, name, immigrants, population, disease_numbers):
        self.name = name
        self.immigrants = immigrants
        self.infected = population
        self.disease_numbers = disease_numbers

    @classmethod
    def add_data(cls, year, *args, **kwargs):
        obj = cls(*args, **kwargs)
        cls.data[obj.name, year] = obj

    # ...

 Country.add_data("Venezuela", 1989, 100, 20, 50)

这很方便,因为与国家数据存储和查询相关的所有功能都可以与您需要的任何建模方法一起存储在国家类中。

vash_the_stampede
Reply   •   2 楼
vash_the_stampede    6 年前
class Country(object):
    """ Modeling a country immigration. """

    def __init__(name, immigrants, population, disease_numbers):

        self.name = name
        self.immigrants = immigrants
        self.infected = population
        self.disease_numbers = disease_numbers

    def update_pop(self, year, rate):
        self.infected = self.infected * year * rate

向类中添加函数是否有效?