I'm having trouble trying to return row 1 of a csv file, which basically contains a string of data: [0, 300, 30, 521, 53, 462, 642]. Basically I'm trying to delete the first column, and then iterate through the first row [300, 30, 521, 53, 462, 642] and append the minimum value (30) into a list called "b". Then repeat that with the second row and second column and so on. However, I get an error and I'm not sure what it means. Can anyone help me with this? Thank you.
Traceback (most recent call last):
File "travelsales.py", line 25, in <module>
nearest(0);
File "travelsales.py", line 17, in nearest
for i in reader.next():
StopIteration
Source code:
import time
import csv
from itertools import islice
b = list();
reader = csv.reader(open('distance.csv','rU'), delimiter = ' ');
def delete(column):
for i in reader:
del i[column];
def nearest(row):
a = list();
list(islice(reader, row));
for i in reader.next():
a.append(int(i));
print a;
b.append(min(a));
del a[:];
print b;
delete(0);
nearest(0);
You exhaust your
reader
in delete - it's similar to reading to the end of a file. This approach is overcomplicated. To meet the revised goal of calculating the min of each row, ignoring the nth column in each nth row, I would do this:Since each
line
list is already created, I think slicing directly is faster thanitertools.islice
. List addition vs.itertools.chain
I'm not so sure about.might also be worth testing.
Also, Python doesn't use semicolons.