Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read list in python [duplicate]

Tags:

python

list

I have this list of lists that looks like this:

vrt = [[1,3,3,8,5,4,2,1,5,6],
       [2,4,3,3,6,8,1,3,5,6],    
       [4,5,6,4,7,4,3,6,4,7],
       [2,8,7,0,0,7,4,7,8,0],
       [2,3,4,7,0,8,7,6,3,8],
       [3,7,9,0,8,5,3,2,3,4],
       [1,5,7,7,6,4,2,3,5,6],
       [0,6,3,3,6,8,0,6,7,7],
       [0,1,3,2,8,0,0,0,0,0],
       [3,1,0,3,6,7,0,5,3,1],
       [1,3,5,7,0,8,6,5,3,1],
       [3,6,3,1,3,5,8,7,5,1],
       [4,3,6,0,0,8,4,7,5,3],
       [3,5,6,8,6,3,1,3,5,2]]

And I need to write a function that will read evey line, sum all the integers in every line and return the result of every line as new list, in case of this particular list result would be [38, 41, 50...42], so just sum of every list.

Up till now I tried doing it like this

def po_vrstah(vrt):
    s = []
    line = vrt.read().split('\n')
    vsota = sum(line)
    s.append(vsota)
    return s

So I did a little research and apparently those read and split ('\n') can be used only reading files I think?

So my question is, how can I use similar approach solving this problem? Is there a way to read lists in similar way as files?

like image 933
Doe Avatar asked Jan 21 '26 03:01

Doe


1 Answers

It's much more simple:

>>> print map(sum, vrt)
like image 171
Simon Sagi Avatar answered Jan 23 '26 15:01

Simon Sagi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!