tags:

views:

64

answers:

2

How can you fix the code?

I am trying to have i % 3 == 1 and i != 16 unsuccessfully by

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

arra = sum(arra[1::3 and != 16]) for i in range(0, len(arra), 16)]        
                       |
                       |---// Problem here
A: 

Slices don't work like that.

Paul McGuire has the correct code:

arra = sum(x for i, x in enumerate(arra) if i % 3 == 1 and i != 16)

It's also not clear from your code what the point of the for i in range(0, len(arra), 16)] is supposed to be. What are you trying to accomplish?

Daniel Pryden
The OP does not want to skip *values* == 16, but the *index* == 16.
Paul McGuire
@Paul McGuire: Ah yes. My mistake. I've corrected my answer.
Daniel Pryden
+5  A: 

Try this:

arra = sum(a for i,a in enumerate(arra) if i %3==1 and i != 16)

For this kind of complex work, slice notation wont really do. But why do you assign back to arra? You wipe out your original list of values.

Paul McGuire
+1 for using `enumerate()`. This is the correct answer.
Daniel Pryden