【发布时间】:2020-11-19 23:31:45
【问题描述】:
这是我需要的输出:
Temperature anomaly filename:SacramentoTemps.csv
Enter window size:60
1940,-0.2331
1941,-0.2169
1942,-0.2150
1943,-0.2228
1944,-0.2107
1945,-0.1796
1946,-0.1667
1947,-0.1582
1948,-0.1585
1949,-0.1492
1950,-0.1711
1951,-0.1688
1952,-0.1490
1953,-0.1556
1954,-0.1548
1955,-0.1580
1956,-0.1420
1957,-0.1101
1958,-0.1017
这是我的代码:
filename = input("Temperature anomaly filename:")
infile = open(filename, "r")
k = int(input("Enter window size:"))
infile.readline()
temp_list = []
for line in infile:
line = line.strip()
year,temp = line.split(",")
temp = float(temp)
temp_list.append(temp)
index = k
for index in range(index,len(temp_list)-1-index):
year = 1880 + index
ave = sum(temp_list[index:index+k]) / (2*index+1)
print(str(year)+","+"{:.4f}".format(ave))
infile.close()
我的代码目前打印到 1957 年,并且每年打印出错误的平均值。我需要解决什么问题?
【问题讨论】:
-
您能否通过向“temp_list = []”添加一些值来稍微改变您的问题(而不是从文本文件中读取,这样更容易重新生成)。?
-
理想情况下,
temp_list中应该有足够的值来匹配您想要的输出。
标签: python moving-average