简体   繁体   English

如何从列表中删除第一项?

[英]How do I remove the first item from a list?

How do I remove the first item from a list?如何从列表中删除第一项?

[0, 1, 2, 3]   →   [1, 2, 3]

You can find a short collection of useful list functions here .您可以在此处找到有用的列表函数的简短集合。

list.pop(index)

>>> l = ['a', 'b', 'c', 'd']
>>> l.pop(0)
'a'
>>> l
['b', 'c', 'd']
>>> 

del list[index]

>>> l = ['a', 'b', 'c', 'd']
>>> del l[0]
>>> l
['b', 'c', 'd']
>>> 

These both modify your original list.这些都修改了您的原始列表。

Others have suggested using slicing:其他人建议使用切片:

  • Copies the list复制列表
  • Can return a subset可以返回一个子集

Also, if you are performing many pop(0) , you should look at collections.deque此外,如果您正在执行许多pop(0) ,您应该查看collections.deque

from collections import deque
>>> l = deque(['a', 'b', 'c', 'd'])
>>> l.popleft()
'a'
>>> l
deque(['b', 'c', 'd'])
  • Provides higher performance popping from left end of the list提供从列表左端弹出的更高性能

Slicing:切片:

x = [0,1,2,3,4]
x = x[1:]

Which would actually return a subset of the original but not modify it.这实际上会返回原始的子集但不会修改它。

>>> x = [0, 1, 2, 3, 4]
>>> x.pop(0)
0

More on this here .更多关于这里

With list slicing, see the Python tutorial about lists for more details:使用列表切片,请参阅关于列表的 Python 教程以获取更多详细信息:

>>> l = [0, 1, 2, 3, 4]
>>> l[1:]
[1, 2, 3, 4]

you would just do this你会这样做

l = [0, 1, 2, 3, 4]
l.pop(0)

or l = l[1:]l = l[1:]

Pros and Cons利弊

Using pop you can retrieve the value使用 pop 你可以检索值

say x = l.pop(0) x would be 0x = l.pop(0) x将是0

Then just delete it:然后删除它:

x = [0, 1, 2, 3, 4]
del x[0]
print x
# [1, 2, 3, 4]

You can use list.reverse() to reverse the list, then list.pop() to remove the last element, for example:您可以使用list.reverse()反转列表,然后使用list.pop()删除最后一个元素,例如:

l = [0, 1, 2, 3, 4]
l.reverse()
print l
[4, 3, 2, 1, 0]


l.pop()
0
l.pop()
1
l.pop()
2
l.pop()
3
l.pop()
4

You can also use list.remove(a[0]) to pop out the first element in the list.您还可以使用list.remove(a[0]) pop列表中的第一个元素。

>>>> a=[1,2,3,4,5]
>>>> a.remove(a[0])
>>>> print a
>>>> [2,3,4,5]

If you are working with numpy you need to use the delete method:如果您正在使用numpy ,则需要使用delete方法:

import numpy as np

a = np.array([1, 2, 3, 4, 5])

a = np.delete(a, 0)

print(a) # [2 3 4 5]

Unpacking assignment:解包任务:

You could use unpacking assignment as mentioned in PEP 3132 .您可以使用PEP 3132中提到的解包分配。

Solution:解决方案:

You should try unpacking like the following:您应该尝试像下面这样解包:

>>> l = [0, 1, 2, 3, 4]
>>> _, *l = l
>>> l
[1, 2, 3, 4]

Explanation:解释:

As mentioned in PEP 3132 :PEP 3132中所述:

This PEP proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not assigned to a "regular" name.此 PEP 提议更改可迭代的解包语法,允许指定一个“包罗万象”的名称,该名称将被分配一个未分配给“常规”名称的所有项目的列表。

An example says more than a thousand words:一个例子说一千多个字:

 >>> a, *b, c = range(5) >>> a 0 >>> c 4 >>> b [1, 2, 3]

There is a data structure called deque or double ended queue which is faster and efficient than a list.有一种称为deque或双端队列的数据结构,它比列表更快更高效。 You can use your list and convert it to deque and do the required transformations in it.您可以使用您的列表并将其转换为双端队列并在其中执行所需的转换。 You can also convert the deque back to list.您还可以将双端队列转换回列表。

import collections
mylist = [0, 1, 2, 3, 4]

#make a deque from your list
de = collections.deque(mylist)

#you can remove from a deque from either left side or right side
de.popleft()
print(de)

#you can covert the deque back to list
mylist = list(de)
print(mylist)

Deque also provides very useful functions like inserting elements to either side of the list or to any specific index. Deque 还提供了非常有用的功能,例如将元素插入到列表的任一侧或任何特定索引。 You can also rotate or reverse a deque.您还可以旋转或反转双端队列。 Give it a try!试试看!

This works for me, instead of using pop like below, which of course will be 0, because the result/return value of pop这对我有用,而不是像下面那样使用pop ,它当然是 0,因为pop的结果/返回值

>>> x = [0, 1, 2, 3].pop(0)
>>> x
0

Using this below method will skip the first value:使用下面的方法将跳过第一个值:

>>> x = [0, 1, 2, 3][1:]
>>> x
[1, 2, 3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM