練習三十二:用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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM