Create a dictionary from all instances of a class OOP python
1 2 3 4 5 6 7 8 9 | 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年年底,我试图建立一本有序的字典,告诉我哪个国家人口最多,疾病最少……我如何建立一个每年/每轮更新的字典,并在最后给我这个信息。
我怎样才能得到最后一回合的数据信息?
我来澄清一下这个问题。
我只需要在模拟的最后有一个有序的字典。
1 2 3 4 5 6 7 8 | 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)} |
然后可以选择例如伦敦,因为有更多的人患有疾病。因此,思考这个问题几乎可以成为一种新的方法,让疾病患者数量更多的城市回归。
不完全清楚您打算如何更新国家/地区数据,但听起来您需要的只是将国家/地区数据对象存储在字典中,使用一系列类似数据库的函数进行查询,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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 ... |
如果需要,可以将这些函数和数据字典作为类方法添加到类中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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) |
这很方便,因为与国家数据存储和查询相关的所有功能都可以与您需要的任何建模方法一起存储在国家类中。
1 2 3 4 5 6 7 8 9 10 11 12 | 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 |
向类中添加函数是否有效?