【发布时间】:2015-07-04 20:26:15
【问题描述】:
字典得到了years 的密钥,并且每年它都是该年所有 12 个月中所有温度的列表。我的目标是打印出一张表格,从年份开始,然后每个月换行和当月的温度。
主要是用(ATH)标记所有年份的最高温度,并用(YearHighest)标记每年的最高温度。
My current code:
temp_dict= {
"2010": [2, 3, 4, 5, 7, 3, 20, 29, 34, 2, 10, 1],
"2011": [2, 7, 4, 5, 9, 3, 20, 9, 34, 2, 10, 10]
}
for key in temp_dict:
print("Year",key,":")
x=0
for temps in temp_dict[key]:
x=x+1
print("Month "+str(x)+":%3d"%temps)
print()
我不确定如何制作 max 函数,我在想这样的事情,但我无法让它工作:
for key in temp_dict:
ATH = temp_dict[key]
YearHigh = temp_dict[key][0]
for temps in temp_dict[key]:
if temps >= temp_dict[key][0]:
YearHigh = temps
if YearHigh >= ATH:
ATH = YearHigh
我希望我的输出是什么样的:
Year 2011 :
Month1: 2
Month2: 7
Month3: 4
Month4: 5
Month5: 9
Month6: 3
Month7: 20
Month8: 9
Month9: 34 (YearHighest)(ATH)
Month10: 2
Month11: 10
Month12: 10
Year 2010 :
Month1: 2
Month2: 3
Month3: 4
Month4: 5
Month5: 7
Month6: 3
Month7: 20
Month8: 29
Month9: 34 (YearHighest)(ATH)
Month10: 2
Month11: 10
Month12: 1
【问题讨论】:
标签: python list python-3.x for-loop dictionary