Skip to content

Latest commit

 

History

History
24 lines (18 loc) · 362 Bytes

File metadata and controls

24 lines (18 loc) · 362 Bytes

方法一:使用切片

>>> a = [1,2,3,4,5]
>>> a[::-1]
[5, 4, 3, 2, 1]

方法二:使用内置函数reversed

>>> list(reversed(a))
[5, 4, 3, 2, 1]

方法三:调用方法list.reverse

>>> a.reverse()
>>> a
[5, 4, 3, 2, 1]

reverse 是对原列表进行反转,前两种方法将生成一个新的列表对象。