python倒叙访问列表

方法一:

names = ['Alice', 'Bob', 'David', 'Ellenal']
for i in range(len(names)-1,-1,-1):
print(names[i])

方法二:

names = ['Alice', 'Bob', 'David', 'Ellenal']
for i in reversed(names):
print(i)

 举例

for i in range(3,-1,-1):
print(i)
输出:3,2,1,0
开始3,步长-1,结束-1,(-1不打印,打印前面一位,即0)

posted @ 2021-12-18 21:18  -夏至未至-  阅读(127)  评论(0编辑  收藏  举报