簡體   English   中英

從類OOP python的所有實例創建字典

[英]Create a dictionary from all instances of a class OOP python

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)}

然后可以在這種情況下選擇倫敦,因為有更多的人患有疾病。 因此,仔細考慮可能幾乎是一種新方法,可以使這個城市的疾病患者人數增加。

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

將功能添加到類上可以嗎?

尚不清楚您打算如何更新國家/地區數據,但是聽起來您需要的只是將國家/地區數據對象存儲在詞典中,並使用一系列類似於數據庫的函數來查詢它,如下所示:

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 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)

這很方便,因為與國家數據的存儲和查詢有關的所有功能都可以與所需的任何建模方法一起存儲在Country類中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM