user124123
user124123

Reputation: 1683

summing elements of a list of csv lists in python

I have a list of csv lists like:

[[0, 0], [0, 2], [1, 1], [2, 0], [0, 3], [1, 2], [2, 1], [3, 0]]

What I want to do is sum each individual list i.e create a new list that is composed of each comma seperated variable summed, and check if they equal some value i.e:

check [0+0,0+2,1+1,2+0..... is equal to some number

I have gotten as far as:

 if sum(gcounter)==3:
   gamma=True
   print(gamma)
 else:
   pass

I have tried sum(int..., and tried using a for loop amongst others but it keeps throwing the same error each time i try a different method TypeError: unsupported operand type(s) for +: 'int' and 'list' So its a problem with the sum function

Trying to work this out has frankly left me feeling listless would greatly appreciate any help!!!

Upvotes: 0

Views: 113

Answers (1)

mgilson
mgilson

Reputation: 310097

Are you looking for something like:

>>> lst = [[0, 0], [0, 2], [1, 1], [2, 0], [0, 3], [1, 2], [2, 1], [3, 0]]
>>> print ([x for x in lst if sum(x) == 3])
[[0, 3], [1, 2], [2, 1], [3, 0]]

Essentially I loop over lst getting one sublist at a time. I sum the sublist and if it equals 3, I keep the sublist in the output list.

Upvotes: 6

Related Questions