问题描述
如果我有一个清单
Mylist = [1,2,3,4,5]
我可以打印它
:
打印我
和结果是
1
2
3
4
5
但是如何以相反的顺序打印所以我得到了
5
4
3
2
1
?
谢谢。
L
If I have a list
Mylist=[1,2,3,4,5]
I can print it
for i in Mylist:
print i
and results is
1
2
3
4
5
But how can I print it in a reverse order so that I get
5
4
3
2
1
?
Thanks.
L
推荐答案
for i in reverse(Mylist):
print i
for i in reversed(Mylist):
print i
使用反向方法反转列表
l.reverse()
对于我在l:
打印我
并在需要时将其反转
reverse the list in place with reverse method
l.reverse()
for i in l:
print i
and the reverse it back if needed
[...]
[...]
另一种选择:
Another option:
.... print i
....
5
4
3
2
1
但是,我认为逆转(Mylist)方式更好。
-
格兰特爱德华兹格兰特哇!机器上哪里有可乐
?告诉我一个笑话!!
visi.com
.... print i
....
5
4
3
2
1
But, I think the reversed(Mylist) way is better.
--
Grant Edwards grante Yow! Where''s the Coke
at machine? Tell me a joke!!
visi.com
这篇关于FOR声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!