在包含列表的dict中查找最大值

时间:2015-04-26 12:54:03

标签: python list python-3.x for-loop dictionary

dict获得了密钥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()

我不确定如何制作最大功能,我正在考虑这样的事情,但我无法让它发挥作用:

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

4 个答案:

答案 0 :(得分:4)

Python内置了函数max,它被认为是一种很好的使用方法。

年度最大值:

max(temp_dict["2010"])

所有时间最长:

max(sum(temp_dict.values(), []))

sum(lists, [])列出扁平化,相当于

[] + lists[0] + lists[1]...

答案 1 :(得分:1)

Python有一个你可以使用的内置函数max

for key in temp_dict:
    print("Year", key,":")
    temps = temp_dict[key]
    max_temp = max(temps)
    max_index = temps.index(max_temp)
    for index, temps in enumerate(temps):
        r = "Month "+str(index+1)+":%3d"%temps
        if index == max_index:
            r += "(YearHighest)(ATH)"
        print(r)

答案 2 :(得分:0)

您可以尝试这样的事情:

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

# defines the max of all years with a list comprehension
global_max_temp = max([ max(year_temps) for year_temps in temp_dict.values() ])

# iterates through each year
for year, temps in temp_dict.items():
    print("Year {}".format(year))
    for i, temp in enumerate(temps):
        # prepares the output
        temp_string = ["Month{}: {}".format(i+1, temp)]

        # builds a list of flags to be displayed
        flags = []
        if temp == max(temps):
            # max in year flag
            flags.append("YearHighest")
        if temp == global_max_temp:
            # absolute max flag
            flags.append("ATH")

        # joins temp_string and flags in a single line and prints it
        print(" ".join(temp_string + [ "({})".format(flag) for flag in flags ]))

Python文档中的有用链接:enumeratelist comprehensionsmax

答案 3 :(得分:-1)

这是我的代码。

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

# Find the highest temp of all years
ath = max([ max(v) for v in temp_dict.values()])

for key in temp_dict:
    # Output Year
    print("Year{k}:".format(k=key))
    x=0
    # Find max
    max_value = max(temp_dict[key])
    for temps in temp_dict[key]:
        # Output Month 
        x=x+1
        s = "Month {x}:{v:3d}".format(x=str(x), v=temps)
        # Tag the max value
        if max_value == temps:
            s += "(YearHighest)" 
        if ath == temps:
            s += "(ATH)"
        print(s)
    print()

这是我的输出。

Year2010:                                                                                                                                             
Month 1:  2                                                                                                                                           
Month 2:  3                                                                                                                                           
Month 3:  4                                                                                                                                           
Month 4:  5                                                                                                                                           
Month 5:  7                                                                                                                                           
Month 6:  3                                                                                                                                           
Month 7: 20                                                                                                                                           
Month 8: 29                                                                                                                                           
Month 9: 34(YearHighest)(ATH)                                                                                                                         
Month 10:  2                                                                                                                                          
Month 11: 10                                                                                                                                          
Month 12:  1                                                                                                                                          

Year2011:                                                                                                                                             
Month 1:  2                                                                                                                                           
Month 2:  7                                                                                                                                           
Month 3:  4                                                                                                                                           
Month 4:  5                                                                                                                                           
Month 5:  9                                                                                                                                           
Month 6:  3                                                                                                                                           
Month 7: 20                                                                                                                                           
Month 8:  9                                                                                                                                           
Month 9: 34(YearHighest)(ATH)                                                                                                                         
Month 10:  2                                                                                                                                          
Month 11: 10                                                                                                                                          
Month 12: 10

这里需要使用max函数。它可以快速从列表的数字中获得最大值。