Python 什么';列表中的值发生了什么变化?
为了自我练习,我正在编写一个字典程序,它将数据存储在以下数据结构中:Python 什么';列表中的值发生了什么变化?,python,Python,为了自我练习,我正在编写一个字典程序,它将数据存储在以下数据结构中:[(平均,月),(平均,月),…(平均,月)]。该数据文件名为table.csv,可在以下链接中找到: 我的问题是,当这个条件变为false时,为什么列表,testList[x][0]变为空白 if dates == UniqueDates[x]: 当x=0,例如testList[0][0],且条件为True,则列表为[474.98、468.22、454.7、455.19、439.76、450.99]。但是,当它变成Fals
[(平均,月),(平均,月),…(平均,月)]
。该数据文件名为table.csv,可在以下链接中找到:
我的问题是,当这个条件变为false时,为什么列表,testList[x][0]
变为空白
if dates == UniqueDates[x]:
当x=0
,例如testList[0][0]
,且条件为True
,则列表为[474.98、468.22、454.7、455.19、439.76、450.99]
。但是,当它变成False
时,同样的列表,testList[0][0]
,神秘地变成了[]
。为什么不保留列表中的值
f = open('table.csv','r').readlines()
col = 6
testList = []
uniqueDates = []
x = 0
for i in range(1,len(f)):
dates = f[i].split(',')[0][:7]
column = float(f[i].split(',')[col])
if dates not in uniqueDates:
uniqueDates.append(dates)
testList.append(())
testList[x] = [],dates
if dates == uniqueDates[x]:
testList[x][0].append(column)
else:
testList[x][0].append((mean(testList[x][0]),uniqueDates[x]))
x += 1
testList[x][0].append(column)
考虑这一节:
if dates not in uniqueDates:
uniqueDates.append(dates)
testList.append(())
testList[x] = [],dates
第一次执行此操作是在处理第7行时,即月份第一次更改。在执行本节之前,x==0
;因此,此块中的最后一行将替换testList
的第一个元素。我认为您希望它替换刚才附加的新空元素
我想你想在这里简单地把最后两行合并成一行:
if dates not in uniqueDates:
uniqueDates.append(dates)
testList.append(([],dates))
是什么意思?在testList[x][0]中添加((平均值(testList[x][0]),唯一日期[x])
?平均值来自名为statistics I imported的模块。