Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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
List 将每月临时工的文本文件放入集合中_List_Python 3.x_File Io_Set - Fatal编程技术网

List 将每月临时工的文本文件放入集合中

List 将每月临时工的文本文件放入集合中,list,python-3.x,file-io,set,List,Python 3.x,File Io,Set,我有一个一年几个月的.txt文件,上面有每天的温度读数 我希望获取从.readlines()创建的列表,并将每个月分开,并消除任何重复值(使用集合) 这是文本文件: January:23,23,23,21,21,23,23,22,22,23,23,23,22,22,22,23,23,23,22,19,22,23,22,22,22,22,22,23,23,23,22 February:23,22,26,26,26,27,27,27,26,26,26,27,27,3,26,26,27,26,26,2

我有一个一年几个月的.txt文件,上面有每天的温度读数

我希望获取从.readlines()创建的列表,并将每个月分开,并消除任何重复值(使用集合)

这是文本文件:

January:23,23,23,21,21,23,23,22,22,23,23,23,22,22,22,23,23,23,22,19,22,23,22,22,22,22,22,23,23,23,22
February:23,22,26,26,26,27,27,27,26,26,26,27,27,3,26,26,27,26,26,26,27,26,26,26,26,26,26
March:19,18,18,18,23,21,31,33,33,22,19,18,18,18,4,5,31,33,19,18,19,18,18,18,23,21,31,33,33,22
April:40,17,17,17,19,19,18,19,22,22,19,19,18,19,23,17,19,5,18,19,17,19,19,18,19,22,22,19,19,18
May:1,19,19,18,19,22,22,19,19,18,36,35,22,22,22,19,33,27,6,23,22,22,19,22,23,23,22,22,19,19
June:33,23,19,18,19,22,22,19,19,18,36,35,22,22,19,19,18,22,19,19,8,36,22,19,19,18,36,35,22
July:23,23,23,23,23,33,33,33,22,22,19,19,18,36,35,49,19,19,18,36,35,9,19,19,18,36,35,22
August:18,23,36,35,49,19,19,18,36,35,49,19,19,18,36,35,22,19,19,18,36,35,49,15,19,18,36,35,22
September:18,36,35,49,19,19,18,36,35,49,19,19,18,36,35,22,23,23,18,36,35,49,14,19,19,18,36,35,22
October:18,36,35,49,19,19,18,36,35,49,19,19,18,36,35,22,23,23,22,22,33,22,19,19,19,19,18,36,35,22
November:18,36,35,49,19,19,18,36,35,49,19,19,18,36,35,22,23,23,23,24,22,22,19,18,36,35,49,19,21,11
December:18,36,35,49,19,19,40,23,22,22,23,18,36,35,49,19,19,18,36,18,36,35,12,19,19,18,23,22,22,23
这就是我到目前为止所做的:

year_weather = None
months = ['January', 'Feburary', 'March', 'April', 'May', 'June', 'July',
          'August', 'September', 'October', 'November', 'December']

def open_weather():
    global year_weather
    weather = open('yearly_temperature.txt')
    year_weather = weather.readlines() 
    return year_weather

def month_temp(year_weather, months):
    output = []
    for x in months:
        for y in year_weather:
            output.append(months[x])
            output.append(year_weather[y])
    return output
我知道我必须创建一个For循环,它以月份为单位获取每个值,并将年份_weather中的每个对应值相加,即月份的一月+年份_weather中该月的值

但我似乎不明白


有什么帮助吗?谢谢

这就是你能做的。我们取每一行,去掉月份和换行符,然后用逗号分开。然后我们把列表转换成一个集合,瞧。此外,无需将
year\u weather
声明为全局变量

months = ['January', 'Feburary', 'March', 'April', 'May', 'June', 'July',
          'August', 'September', 'October', 'November', 'December']

def open_weather():
    weather = open('yearly_temperatures.txt')
    year_weather = weather.readlines() 
    return year_weather

def month_temp(year_weather, months):
    output = []
    for i in range(len(months)):
        output.append((months[i], set(year_weather[i].lstrip(months[i] + ":").strip("\n").split(","))))
    return output

yw = open_weather()
print(month_temp(yw, months))