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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4426663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:41:29  来源:igfitidea点击:

How to remove the first Item from a list?

pythonarrayslist

提问by rectangletangle

I have the list [0, 1, 2, 3, 4]I'd like to make it into [1, 2, 3, 4]. How do I go about this?

我有[0, 1, 2, 3, 4]我想加入的清单[1, 2, 3, 4]。我该怎么做?

采纳答案by kevpie

Python List

Python列表

list.pop(index)

list.pop(索引)

>>> 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
  • 提供从列表左端弹出的更高性能

回答by user225312

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

More on this here.

更多关于这里

回答by justin.m.chase

Slicing:

切片:

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

Which would actually return a subset of the original but not modify it.

这实际上会返回原始的子集但不会修改它。

回答by Haes

With list slicing, see the Python tutorial about listsfor more details:

使用列表切片,请参阅关于列表的 Python 教程以获取更多详细信息:

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

回答by Zimm3r

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)xwould be 0

x = l.pop(0)x0

回答by martineau

Then just delete it:

然后删除它:

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

回答by vertexion

You can also use list.remove(a[0])to popout 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]

回答by Yoni Shperling

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

回答by tsveti_iko

If you are working with numpyyou need to use the deletemethod:

如果您正在使用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]

回答by Bhaskar Bhuyan

There is a datastructure called "deque" or double ended queue which is faster and efficient than a list. 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.

有一种称为“deque”或双端队列的数据结构,它比列表更快、更高效。您可以使用您的列表并将其转换为双端队列并在其中执行所需的转换。您还可以将双端队列转换回列表。

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. You can also rotate or reverse a deque. Give it a try!!

Deque 还提供了非常有用的功能,例如将元素插入到列表的任一侧或任何特定索引。您还可以旋转或反转双端队列。试一试!!