练习三十二:用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