简体   繁体   中英

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 ie create a new list that is composed of each comma seperated variable summed, and check if they equal some value ie:

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!!!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM