开发者

Modulus in Python's slicing

开发者 https://www.devze.com 2022-12-10 13:50 出处:网络
How can you fix the following code? I want to get the slice of elements that are i mod 5 == 1开发者_如何学运维.

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)] 


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

And it's spelled array. ;-)


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

0

精彩评论

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