开发者

AND in Python's slicing with modulus

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), 1开发者_StackOverflow社区6)]        
                       |
                       |---// Problem here


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜