Reputation: 1512
For self-practice, I'm writing a dictionary program that stores data in the following data structure: [(average,month),(average,month),....,(average,month)]
. The datafile is called table.csv and can be found in the link:
http://www.cse.msu.edu/~cse231/PracticeOfComputingUsingPython/05_ListsTuples/AppleStock/
The question I have is why does the list, testList[x][0]
, go blank when this condition becomes false?:
if dates == UniqueDates[x]:
When x = 0
, such that testList[0][0]
, and the condition is True
, the list is [474.98, 468.22, 454.7, 455.19, 439.76, 450.99]
. But, when it becomes False
, that same list, testList[0][0]
, mysteriously becomes [ ]
. Why aren't the values in the list being kept?
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)
Upvotes: 3
Views: 73
Reputation: 48121
Consider this section:
if dates not in uniqueDates:
uniqueDates.append(dates)
testList.append(())
testList[x] = [],dates
The first time this executes is when processing line 7, the first time the month changes. Before executing this section, x == 0
; so the last line in this block replaces the first element of testList
. I think you want it to replace the new empty element that you just appended.
I suspect what you want here is to simply combine the last two lines into one:
if dates not in uniqueDates:
uniqueDates.append(dates)
testList.append(([],dates))
Upvotes: 2