Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-2D列表过滤_Python_Python 3.x_List - Fatal编程技术网

Python-2D列表过滤

Python-2D列表过滤,python,python-3.x,list,Python,Python 3.x,List,我创建了一个程序,可以定义12个月的最高、最低和平均温度,但我想通过查找平均温度最高和最低的月份来扩展它。我目前的代码是: def main(): months = [ ["January", 6,3], ["February", 7,3], ["March", 10,4], ["April", 13,6],

我创建了一个程序,可以定义12个月的最高、最低和平均温度,但我想通过查找平均温度最高和最低的月份来扩展它。我目前的代码是:

def main():

    months = [ ["January", 6,3],
               ["February", 7,3],
               ["March", 10,4],
               ["April", 13,6],
               ["May", 17,9],
               ["June", 20,12],
               ["July", 22,14],
               ["August", 21,14],
               ["September",19,12],
               ["October", 14,9],
               ["November", 10,6],
               ["December", 7,3] ]
    
    
    for month in months:
        month_name = month[0]
        temp = month[1:]
        
        minTemp = min(temp)
        avgTemp = sum(temp)/len(temp)
        maxTemp = max(temp)

        print (month_name, minTemp, avgTemp, maxTemp)


    
    
main()


我想添加一些类似“如果<代码>月名具有最高平均温度,则打印<代码>月名及其温度。如果<代码>月名具有最低平均温度,则打印<代码>月名及其温度

将平均温度附加到每个月,并找到最小/最大值

months = [["January", 6, 3],
          ["February", 7, 3],
          ["March", 10, 4],
          ["April", 13, 6],
          ["May", 17, 9],
          ["June", 20, 12],
          ["July", 22, 14],
          ["August", 21, 14],
          ["September", 19, 12],
          ["October", 14, 9],
          ["November", 10, 6],
          ["December", 7, 3]]
for m in months:
    m.append((m[1] + m[2]) / 2)

_min_avg = min(months, key = lambda k: k[3])
_max_avg = max(months, key = lambda k: k[3])
print(_min_avg)
print(_max_avg)
导入数学
def main():
月份=[“一月”,6,3],
[“2月”,7,3],
[“三月”,10,4],
[“4月”,13,6],
[“5月17日,9日],
[“6月”,20日,12日],
[“7月22日、14日],
[“8月21日、14日],
[“9月19日、12日],
[“10月”,14,9],
[“11月”,10,6],
[“12月”,7,3]]
mn,mx=math.inf,-math.inf
mx_月=[“”,0]
月份=[“”,0]
月份中的月份:
月份\u名称=月份[0]
温度=月份[1:]
最小温度=最小(温度)
avgTemp=总和(温度)/长度(温度)
最大温度=最大(温度)
如果avgTempmx:
mx_月[0],mx_月[1]=月名称,avgTemp
mx=avgTemp
打印(月份名称、minTemp、avgTemp、avgTemp)
打印(“最小平均温度月和温度:”,mn_月)
打印(“最大平均温度月和温度:”,mx_月)
main()

你让他改变他的名单,这可能是必要的elsewhere@DanielMeltzer-我正在向每个月的数据中添加一项。我不会修改数据。
max(((m[1]+m[2])/2,m[0])表示m个月)
=>
(18.0,'July')
。是的!这很有效,而且很清楚,可以理解!非常感谢!
import math
def main():

    months = [ ["January", 6,3],
               ["February", 7,3],
               ["March", 10,4],
               ["April", 13,6],
               ["May", 17,9],
               ["June", 20,12],
               ["July", 22,14],
               ["August", 21,14],
               ["September",19,12],
               ["October", 14,9],
               ["November", 10,6],
               ["December", 7,3] ]
    
    mn,mx=math.inf, -math.inf
    mx_month=["",0]
    mn_month=["",0]
    for month in months:
        month_name = month[0]
        temp = month[1:]
        
        minTemp = min(temp)
        avgTemp = sum(temp)/len(temp)
        maxTemp = max(temp)
        if avgTemp<mn:
            mn_month[0],mn_month[1]=month_name,avgTemp
            mn=avgTemp
        if avgTemp>mx:
            mx_month[0],mx_month[1]=month_name,avgTemp
            mx=avgTemp
        print (month_name, minTemp, avgTemp, avgTemp)

    print("Min avg temo month and temp: ",mn_month)
    print("Max avg temo month and temp: ",mx_month)
    
    
main()