如何在不使用循环的情况下反转Python中列表的顺序?

时间:2023-01-23 21:09:16

How can one reverse the order of a list in Python without using a loop? There are no other constraints on the solution space.

如何在不使用循环的情况下反转Python中列表的顺序?解决方案空间没有其他限制。

6 个解决方案

#1


6  

a = ['a','b','c']

you can try

你可以试试

b = a[::-1]

It will reverse the list without using loop.

它将在不使用循环的情况下反转列表。

You can use the same trick on any sequence/container/iterable to get item reversed.

您可以在任何序列/容器/可迭代上使用相同的技巧来使项目反转。

#2


4  

list.reverse()

list.reverse()

Whether or not the underlying implementation uses a loop I don't know.

底层实现是否使用循环我不知道。

Datastructures - Python Documentation

数据结构 - Python文档

#3


2  

This reverse function is inefficient because of the way Python handles lists. However, it's worth understanding because it shows how to use a recursive function to replace a loop in a way that can be adapted to almost any language. (In other words, it doesn't depend on built-in features of Python.) Note that l[:1] returns a single-item list containing the first item of l and l[1:] returns all the remaining items in l.

由于Python处理列表的方式,这种反向函数效率低下。但是,它值得理解,因为它展示了如何使用递归函数以几乎适用于任何语言的方式替换循环。 (换句话说,它不依赖于Python的内置功能。)请注意,l [:1]返回包含l和l的第一项的单项列表[1:]返回所有剩余项目湖

>>> def reverse(l):
...     return reverse(l[1:]) + l[:1] if l else l
... 
>>> reverse([1, 2, 3, 4, 5])
[5, 4, 3, 2, 1]

#4


1  

You can also use the builtin function reversed

您也可以使用内置函数反转

a = list(reversed(your_list))

#5


1  

Assuming you want the change in-place:

假设你想要就地改变:

>>> a = [1,2,3]
>>> a.reverse()
>>> a
[3, 2, 1]
>>> a[:] = a[::-1]
>>> a
[1, 2, 3]

#6


1  

There are many ways:

有很多方法:

You can use extended List Slicing:

您可以使用扩展列表切片:

>>> print [1, 2, 3, 4, 5][::-1]
[5, 4, 3, 2, 1]

Or use the built in .reverse() function:

或者使用内置的.reverse()函数:

>>> L = [1, 2, 3, 4, 5]
>>> L.reverse()
>>> print L
[5, 4, 3, 2, 1]

Or use reversed(), which returns the reversed list - but as an iterator:

或者使用reversed(),它返回反转列表 - 但作为迭代器:

>>> L = [1, 2, 3, 4, 5]
>>> print reversed(L)
<listreverseiterator object at 0x2c3630>
>>> print list(reversed(L))
[5, 4, 3, 2, 1]

Timing comparisons:

时间比较:

$ python -m timeit "L = range(1000)" "L = L[::-1]"
100000 loops, best of 3: 11.8 usec per loop

$ python -m timeit "L = range(1000)" "L.reverse()"
100000 loops, best of 3: 9.13 usec per loop

$ python -m timeit "L = range(1000)" "L = list(reversed(L))"
100000 loops, best of 3: 19.1 usec per loop

#1


6  

a = ['a','b','c']

you can try

你可以试试

b = a[::-1]

It will reverse the list without using loop.

它将在不使用循环的情况下反转列表。

You can use the same trick on any sequence/container/iterable to get item reversed.

您可以在任何序列/容器/可迭代上使用相同的技巧来使项目反转。

#2


4  

list.reverse()

list.reverse()

Whether or not the underlying implementation uses a loop I don't know.

底层实现是否使用循环我不知道。

Datastructures - Python Documentation

数据结构 - Python文档

#3


2  

This reverse function is inefficient because of the way Python handles lists. However, it's worth understanding because it shows how to use a recursive function to replace a loop in a way that can be adapted to almost any language. (In other words, it doesn't depend on built-in features of Python.) Note that l[:1] returns a single-item list containing the first item of l and l[1:] returns all the remaining items in l.

由于Python处理列表的方式,这种反向函数效率低下。但是,它值得理解,因为它展示了如何使用递归函数以几乎适用于任何语言的方式替换循环。 (换句话说,它不依赖于Python的内置功能。)请注意,l [:1]返回包含l和l的第一项的单项列表[1:]返回所有剩余项目湖

>>> def reverse(l):
...     return reverse(l[1:]) + l[:1] if l else l
... 
>>> reverse([1, 2, 3, 4, 5])
[5, 4, 3, 2, 1]

#4


1  

You can also use the builtin function reversed

您也可以使用内置函数反转

a = list(reversed(your_list))

#5


1  

Assuming you want the change in-place:

假设你想要就地改变:

>>> a = [1,2,3]
>>> a.reverse()
>>> a
[3, 2, 1]
>>> a[:] = a[::-1]
>>> a
[1, 2, 3]

#6


1  

There are many ways:

有很多方法:

You can use extended List Slicing:

您可以使用扩展列表切片:

>>> print [1, 2, 3, 4, 5][::-1]
[5, 4, 3, 2, 1]

Or use the built in .reverse() function:

或者使用内置的.reverse()函数:

>>> L = [1, 2, 3, 4, 5]
>>> L.reverse()
>>> print L
[5, 4, 3, 2, 1]

Or use reversed(), which returns the reversed list - but as an iterator:

或者使用reversed(),它返回反转列表 - 但作为迭代器:

>>> L = [1, 2, 3, 4, 5]
>>> print reversed(L)
<listreverseiterator object at 0x2c3630>
>>> print list(reversed(L))
[5, 4, 3, 2, 1]

Timing comparisons:

时间比较:

$ python -m timeit "L = range(1000)" "L = L[::-1]"
100000 loops, best of 3: 11.8 usec per loop

$ python -m timeit "L = range(1000)" "L.reverse()"
100000 loops, best of 3: 9.13 usec per loop

$ python -m timeit "L = range(1000)" "L = list(reversed(L))"
100000 loops, best of 3: 19.1 usec per loop