1. 程式人生 > >練習三十二:用python實現:按相反的順序輸出列表的每一位值

練習三十二:用python實現:按相反的順序輸出列表的每一位值

用python實現:按相反的順序輸出列表的每一位值

1. 使用list[::-1]

1 list1 = ["one","two","three","four"]
2 for i in list1[::-1]:#list[::-1]結果為列表的反向
3     print(i)

2. 使用list方法reverse()

1 list1 = ["one","two","three","four"]
2 list1.reverse()
3 for i in list1:
4     print(i)

3. 使用python內建函式

1 list1 = ["
one","two","three","four"] 2 list2 =reversed(list1) 3 for i in list2: 4 print(i)

執行結果:

four
three
two
one