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)}
然后在这种情况下可以选择伦敦,因为有更多的人患有这种疾病。因此,仔细考虑可能几乎是一种新方法,可以使这个城市的疾病患者人数增加。
答案 0 :(得分:1)
TimeZone timezone = TimeZone.getTimeZone("Europe/London");
formatter.setTimeZone(timeZone);
java.sql.Time timeValue = new java.sql.Time(formatter.parse(String.valueOf(model.getStartTime())).getTime())
是否要在类工作中添加一个函数?
答案 1 :(得分:1)
尚不清楚您打算如何更新国家/地区数据,但是听起来您需要的只是将国家/地区数据对象存储在字典中,并使用一系列类似于数据库的函数来查询它,就像这样:
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)
这很方便,因为与国家数据的存储和查询有关的所有功能都可以与所需的任何建模方法一起存储在Country类中。