方式一:调用列表的reverse方法,这样会改变原有列表。
l=[x for x in range(5)] l =[0,1,2,3,4]
l.reverse() l =[4,3,2,1,0]
for x in l:
print(x)
方式二: 利用切片,步进值为-1.
l=[x for x in range(5)] l= [0,1,2,3,4]
l2=l[::-1] l2= [4,3,2,1,0]
for x in l2:
print(x)
方式三:利用reversed函数
l=[x for x in range(5)] l= [0,1,2,3,4]
l3=reversed(l) l3=<list_reverseiterator object at 0x0000018520DC8898> 返回一个迭代器
for x in l3:
print(x)