我有3年的统计数据存储在3个词典中。我希望在3年内找到每个月的最小值。例如,1月份是10,而2月份则是15。
我需要将结果存储在新词典中。例如,{" 1月&#34 ;: 12," 2月和#34 ;: 15,...}
我该怎么办?
stat2011 = {"January": 12, "February": 20, "March": 50, "April": 70, "May": 15,
"June": 35, "July": 30, "August": 15, "September": 20, "October": 60,
"November": 13, "December": 50}
stat2012 = {"January": 36, "February": 15, "March": 50, "April": 10, "May": 90,
"June": 25, "July": 35, "August": 15, "September": 20, "October": 30,
"November": 10, "December": 25}
stat2013 = {"January": 10, "February": 60, "March": 90, "April": 10, "May": 80,
"June": 50, "July": 30, "August": 15, "September": 20, "October": 75,
"November": 60, "December": 15}
答案 0 :(得分:5)
In [2]: stats=[stat2011, stat2012, stat2013]
In [3]: min(i['January'] for i in stats)
Out[3]: 10
In [4]: min(i['February'] for i in stats)
Out[4]: 15
如果你想将每个月的最小值存储在一个字典中:
In [8]: {k:min(i[k] for i in stats) for k in stat2011}
Out[8]:
{'April': 10,
'August': 15,
'December': 15,
'February': 15,
'January': 10,
'July': 30,
'June': 25,
'March': 50,
'May': 15,
'November': 10,
'October': 30,
'September': 20}
答案 1 :(得分:4)
使用集合有一种巧妙的方法。
from collections import Counter
stat2011 = {"January": 12, "February": 20, "March": 50, "April": 70, "May": 15,
"June": 35, "July": 30, "August": 15, "September": 20, "October": 60,
"November": 13, "December": 50}
stat2012 = {"January": 36, "February": 20, "March": 50, "April": 10, "May": 90,
"June": 25, "July": 35, "August": 15, "September": 20, "October": 30,
"November": 10, "December": 25}
stat2013 = {"January": 10, "February": 60, "March": 90, "April": 10, "May": 80,
"June": 50, "July": 30, "August": 15, "September": 20, "October": 75,
"November": 60, "December": 15}
print dict(Counter(stat2011) & Counter(stat2012) & Counter(stat2013)) # MIN
print dict(Counter(stat2011) | Counter(stat2012) | Counter(stat2013)) # MAX
答案 2 :(得分:2)
months = ['February', 'October', 'January', 'April', 'November',
'March', 'August', 'May', 'December', 'June', 'September', 'July']
result, data = {}, [stat2011, stat2012, stat2013]
for month in months:
result.setdefault(month, {})
result[month]["max"] = max(year_data[month] for year_data in data)
result[month]["min"] = min(year_data[month] for year_data in data)
print result
<强>输出强>
{'April': {'max': 70, 'min': 10},
'August': {'max': 15, 'min': 15},
'December': {'max': 50, 'min': 15},
'February': {'max': 60, 'min': 15},
'January': {'max': 36, 'min': 10},
'July': {'max': 35, 'min': 30},
'June': {'max': 50, 'min': 25},
'March': {'max': 90, 'min': 50},
'May': {'max': 90, 'min': 15},
'November': {'max': 60, 'min': 10},
'October': {'max': 75, 'min': 30},
'September': {'max': 20, 'min': 20}}
您可以像这样使用collections.Counter
来获取最高和最低
要获得最大值,
print reduce(lambda x, y: x | Counter(y), data, Counter())
# Counter({'March': 90, 'May': 90, 'October': 75, 'April': 70, 'February': 60, 'November': 60, 'June': 50, 'December': 50, 'January': 36, 'July': 35, 'September': 20, 'August': 15})
获得最低
print reduce(lambda x, y: x & Counter(y), data[1:], Counter(data[0]))
# Counter({'March': 50, 'October': 30, 'July': 30, 'June': 25, 'February': 20, 'September': 20, 'August': 15, 'May': 15, 'December': 15, 'January': 10, 'April': 10, 'November': 10})
答案 3 :(得分:2)
这是一种方法:
minstat = {}
for month in stat2011:
minstat[month] = min(stat2011[month], stat2012[month], stat2013[month])
其他答案比我的更聪明,但有时候最聪明的解决方案最容易阅读:)