a = [1,3,6,8,9] print("經過下標逆序遍歷1:") for i in a[::-1]: print(i, end=" ") print("\n經過下標逆序遍歷2:") for i in range(len(a)-1,-1,-1): print(a[i], end=" ") print("\n經過reversed逆序遍歷:") for i in reversed(a): print(i, end=" ")
輸出python
經過下標逆序遍歷1: 9 8 6 3 1 經過下標逆序遍歷2: 9 8 6 3 1 經過reversed逆序遍歷: 9 8 6 3 1
字符串的逆序遍歷和列表同樣。code