Python中列表和字符串的逆序遍历

2022-09-20 13:47:18

例子1:

a=[1,3,6,8,9]print("通过下标逆序遍历1:")for iin a[::-1]:print(i, end=" ")
# 输出结果:
通过下标逆序遍历198631

例子2:

a=[1,3,6,8,9]print("\n通过下标逆序遍历2:")for iinrange(len(a)-1,-1,-1):print(a[i], end=" ")
# 输出结果:
通过下标逆序遍历298631

例子3:

a=[1,3,6,8,9]print("\n通过reversed逆序遍历:")for iinreversed(a):print(i, end=" ")
# 输出结果:
通过reversed逆序遍历:98631
  • 作者:__泡泡茶壶
  • 原文链接:https://blog.csdn.net/luoxuexi2020/article/details/115911881
    更新时间:2022-09-20 13:47:18