如何在Python中反转列表? [英] How can I reverse a list in Python?

查看:53
本文介绍了如何在Python中反转列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python中执行以下操作?

How can I do the following in Python?

array = [0, 10, 20, 40]
for (i = array.length() - 1; i >= 0; i--)

我需要一个数组的元素,但是要从头到尾.

I need to have the elements of an array, but from the end to the beginning.

推荐答案

您可以使用

You can make use of the reversed function for this as:

>>> array=[0,10,20,40]
>>> for i in reversed(array):
...     print(i)

请注意,reversed(...)不会返回列表.您可以使用list(reversed(array))获得反向列表.

Note that reversed(...) does not return a list. You can get a reversed list using list(reversed(array)).

这篇关于如何在Python中反转列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆