Suppose I have a list
a = [1,2,3,4,5,6,7]
I want alternate digits starting from second to last ie [6,4,2]
.
I tried a[:-1:-2]
but I dont get the correct output.
>>> a = [1,2,3,4,5,6,7]
>>> a[-2::-2]
[6, 4, 2]
You need to specify the slice in the format list[start:end:step]
so use the following instead:
a[-2::-2] # start at the second to last, to to the end by backing up 2
You are in fact going from at the beginning to the end stepping back by twos, thus getting nothing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.