Is there a pythonic way of unpack a list in the first element and the "tail" in a single command?
有没有pythonic方法解压缩第一个元素中的列表和单个命令中的“尾部”?
For example:
例如:
>> head, tail = **some_magic applied to** [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>> head
1
>>> tail
[1, 2, 3, 5, 8, 13, 21, 34, 55]
4 个解决方案
#1
138
Under Python 3.x, you can do this nicely:
在Python 3.x下,你可以很好地做到这一点:
>>> head, *tail = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> head
1
>>> tail
[1, 2, 3, 5, 8, 13, 21, 34, 55]
A new feature in 3.x is to use the *
operator in unpacking, to mean any extra values. It is described in PEP 3132 - Extended Iterable Unpacking. This also has the advantage of working on any iterable, not just sequences.
3.x中的一个新功能是在解包时使用*运算符,表示任何额外的值。它在PEP 3132 - 扩展可迭代解包中进行了描述。这也具有处理任何可迭代的优点,而不仅仅是序列。
It's also really readable.
它也非常易读。
As described in the PEP, if you want to do the equivalent under 2.x (without potentially making a temporary list), you have to do this:
如PEP中所述,如果要在2.x下执行等效操作(不可能创建临时列表),则必须执行以下操作:
it = iter(iterable)
head = it.next()
tail = list(it)
Naturally, if you are working on a list, the easiest way without the 3.x syntax is:
当然,如果您正在处理列表,没有3.x语法的最简单方法是:
head, tail = seq[0], seq[1:]
#2
32
>>> mylist = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> head, tail = mylist[0], mylist[1:]
>>> head
1
>>> tail
[1, 2, 3, 5, 8, 13, 21, 34, 55]
#3
8
For O(1) complexity of head,tail
operation you should use deque
however.
对于O(1)头部,尾部操作的复杂性,您应该使用deque。
Following way:
以下方式:
from collections import deque
l = deque([1,2,3,4,5,6,7,8,9])
head, tail = l.popleft(), l
It's useful when you must iterate through all elements of the list. For example in naive merging 2 partitions in merge sort.
当您必须遍历列表的所有元素时,它很有用。例如,在合并排序中的天真合并2个分区。
#4
3
Python 2, using lambda
Python 2,使用lambda
>>> head, tail = (lambda lst: (lst[0], lst[1:]))([1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
>>> head
1
>>> tail
[1, 2, 3, 5, 8, 13, 21, 34, 55]
#1
138
Under Python 3.x, you can do this nicely:
在Python 3.x下,你可以很好地做到这一点:
>>> head, *tail = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> head
1
>>> tail
[1, 2, 3, 5, 8, 13, 21, 34, 55]
A new feature in 3.x is to use the *
operator in unpacking, to mean any extra values. It is described in PEP 3132 - Extended Iterable Unpacking. This also has the advantage of working on any iterable, not just sequences.
3.x中的一个新功能是在解包时使用*运算符,表示任何额外的值。它在PEP 3132 - 扩展可迭代解包中进行了描述。这也具有处理任何可迭代的优点,而不仅仅是序列。
It's also really readable.
它也非常易读。
As described in the PEP, if you want to do the equivalent under 2.x (without potentially making a temporary list), you have to do this:
如PEP中所述,如果要在2.x下执行等效操作(不可能创建临时列表),则必须执行以下操作:
it = iter(iterable)
head = it.next()
tail = list(it)
Naturally, if you are working on a list, the easiest way without the 3.x syntax is:
当然,如果您正在处理列表,没有3.x语法的最简单方法是:
head, tail = seq[0], seq[1:]
#2
32
>>> mylist = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> head, tail = mylist[0], mylist[1:]
>>> head
1
>>> tail
[1, 2, 3, 5, 8, 13, 21, 34, 55]
#3
8
For O(1) complexity of head,tail
operation you should use deque
however.
对于O(1)头部,尾部操作的复杂性,您应该使用deque。
Following way:
以下方式:
from collections import deque
l = deque([1,2,3,4,5,6,7,8,9])
head, tail = l.popleft(), l
It's useful when you must iterate through all elements of the list. For example in naive merging 2 partitions in merge sort.
当您必须遍历列表的所有元素时,它很有用。例如,在合并排序中的天真合并2个分区。
#4
3
Python 2, using lambda
Python 2,使用lambda
>>> head, tail = (lambda lst: (lst[0], lst[1:]))([1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
>>> head
1
>>> tail
[1, 2, 3, 5, 8, 13, 21, 34, 55]