Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

python - 如何在Python中反转列表?(How can I reverse a list in Python?)

How can I do the following in Python?

(如何在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.

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

  ask by Leo.peis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(您可以通过以下方式使用reversed功能:)

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

Note that reversed(...) does not return a list.

(请注意, reversed(...)不会返回列表。)

You can get a reversed list using list(reversed(array)) .

(您可以使用list(reversed(array))获得反向列表。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...