tags:

views:

76

answers:

2

How can you fix the following code?

I want to get the slice of elements that are i mod 5 == 1.

data = "8|9|8|9|8|9|8|9|9|8|9|8|9|8|9|8" 
arra = map(int,data.split("|"))  

sums += [sum(arra[i % 5==1:(i + 4) % 5==1])         // Problem here
        for i in range(0, len(arra), 4)]
+6  A: 
sums += sum(arra[1::5])

And it's spelled array. ;-)

John Kugelman
I had no idea you could do this. Awesome!
Matt Baker
Further documentation can be found under the term "slicing."
jcdyer
A: 

It's

sums = sum(arra[1::5])

If you use +=, Python spects that the name sums is alreadey accesible:

Traceback (most recent call last): File "", line 1, in sums += sum(arra[1::5]) NameError: name 'sums' is not defined

Juanjo Conti