vendredi 19 décembre 2014

Python: making one list that satisfies the condition

I have a csv file containing the following data:



1,1,3,2,2,1
2,1,3,2,1,1
3,2,4,2,7,3
4,1,3,2,3,1
5,3,3,1,1,1


and I would like to create a list that has the elements of sixth column which has 1 for second column. In short, the end result I would like is [1, 1, 1]


Here is the code I wrote:


input = open("C:\test.txt", "r")



for line in input.readlines():
line = line.strip()
no, grade, gpa, sex, preview, review = line.split(',')

listno1 = []
a = float(grade)
if a == 1:
listno1.append(int(review))


When I print, it turns out like this:



[]
[]
[]
[]
[1]
[2]
[]
[]
[4]
[]


Help please? And I would kind of like to stick to using lists. I'm a beginner so other codes are still a little confusing ;_; patience would be much appreciated!


Aucun commentaire:

Enregistrer un commentaire