Modulus in Python's slicing
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
精彩评论