Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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 比较字典中两个键的值并保存最大值_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 比较字典中两个键的值并保存最大值

Python 比较字典中两个键的值并保存最大值,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,因此,我从一个列表中获取dictionary key:value对,并希望从中创建一个dictionary。 它们的键是年,但每年发生12次,我希望每年的最高值存储在我的字典中。这是最热的一天,代表了那一年最热的一天。我还添加了一个数据的示例屏幕截图 import os file_list = os.listdir('./weatherdata/') hottest_day = {} for file_name in file_list: max_temp = -99 with

因此,我从一个列表中获取dictionary key:value对,并希望从中创建一个dictionary。 它们的键是年,但每年发生12次,我希望每年的最高值存储在我的字典中。这是最热的一天,代表了那一年最热的一天。我还添加了一个数据的示例屏幕截图

import os
file_list = os.listdir('./weatherdata/')

hottest_day = {}
for file_name in file_list:
    max_temp = -99
    with open('weatherdata/' + file_name) as f:
        line = f.read().splitlines()
        req_line = line[2:-1]
        for lines in req_line:
            lines = lines.split(',')
            year = lines[0].split('-')[0]
            try:
                temp = int(lines[1])
            except ValueError:
                continue
            if temp >= max_temp:
                max_temp = temp
            if year in hottest_day:                 
                hottest_day[year] = max_temp
print(hottest_day)
这是到目前为止我的代码,请告诉我这是否足够。 我现在得到这个,这显然是它找到的最后一个键,我需要比较值并打印出具有最高值的键

{'2007':43,'2010':44,'2006':23,'1997':41,'2001':37,'2008':30,'2003':28,'1998':31,'2005':30,'2009':31,'2002':42,'2011':22,'1999':21,'2000':44,'1996':24,'2004':41}


输入文件屏幕截图的一部分:

基本上,您的主要问题是您需要每年的最高温度,但您在所有年份都使用一个
max_temp
变量。你需要为每年保持一个不同的“最高温度”。这里一个简单的解决方案是使用
defaultdict
表示
最热的一天
,初始值为
-99
“max\u temp”作为默认值:

from collections import defaultdict

hottest_day = defaultdict(lambda: -99)

for file_name in file_list:
    with open('weatherdata/' + file_name) as f:
        # nb: this is a list of lines so it should be a plural
        # nb2: beware, you're reading the whole file in memory,
        # this can crash on huge files.
        lines = f.read().splitlines()

    lines = lines[2:-1]
    for line in lines:
        # nb : python as a `csv` module, which might be safer
        line = line.split(',')
        year = line[0].split('-')[0]
        try:
            temp = int(line[1])
        except ValueError as e:
            # you may want to log the error somewhere
            # for debugging...
            continue
        # defaultdict magic: if `year` has
        # not been set yet, it will default to -99
        if temp >= hottest_day[year]:
            hottest_day[year] = temp

很抱歉,没有足够的上下文。你的输入是什么样子的?
temp
来自哪里?注意,请发布一个合适的MCVE,我从一堆csv文件中获取数据,这些文件是按月计算的温度,将其放入列表中并从中获取数据。年份是文件的一列,临时工是另一列。如果你愿意,我可以从文件中发布更多的代码或数据。我的第一个问题因为内容太多而结束了。你也读了我写的那页吗?“MCVE”指“最小完整可验证示例”。你的第一篇文章显然在“最低”标准上失败了,现在这篇文章在“完整”和“可验证”标准上失败了。您必须至少发布一个示例输入列表(尽可能短,但不短于重现问题所需的长度)和作用于该列表的完整代码(可能编辑掉不相关的操作)。@Brunodesshuilliers我编辑过它,请查看这是否解决了问题。另外,请友善一点,我是新来的,对编程也不熟悉。我很友善-我善意地询问了缺失的部分,将你链接到一个页面,解释如果你希望得到有用的答案需要什么,第二次解释了MCVE的概念,所有这些都花费了我时间,我没有得到报酬。我本可以投票关闭你的帖子,然后在没有任何解释的情况下对它投了否决票,或者发布了一个基于胡乱猜测的完全虚假的答案。