【发布时间】:2019-03-10 11:08:07
【问题描述】:
我知道我可以让for i in x[::-1]: 以相反的顺序遍历列表。但是如果我想跳过最后一个元素然后从高到低遍历呢? for i in x[-1:0:-1] 不起作用
x = [1,2,3,4,5]
=> 4,3,2,1 # desired
for i in x[-1:-1]:
print(i)
# it does not work
【问题讨论】:
-
你可以使用这个:
x[-2::-1] -
快速提问是否要使用shortand for if elif 语句而不使用else?
标签: python python-3.x list indexing reverse