问题:如何从列表中删除第一个项目?
我有表[0, 1, 2, 3, 4]
,我想将它做成[1, 2, 3, 4]
。我该怎么办?
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
Python清单
list.pop(索引)
>>> l = ['a', 'b', 'c', 'd']
>>> l.pop(0)
'a'
>>> l
['b', 'c', 'd']
>>>
删除列表[索引]
>>> l = ['a', 'b', 'c', 'd']
>>> del l[0]
>>> l
['b', 'c', 'd']
>>>
这些都将修改您的原始列表。
其他人建议使用切片:
另外,如果要执行许多pop(0),则应查看collections.deque
from collections import deque
>>> l = deque(['a', 'b', 'c', 'd'])
>>> l.popleft()
'a'
>>> l
deque(['b', 'c', 'd'])
Python List
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
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
回答 1
切片:
x = [0,1,2,3,4]
x = x[1:]
这实际上将返回原始的子集,但不会对其进行修改。
Slicing:
x = [0,1,2,3,4]
x = x[1:]
Which would actually return a subset of the original but not modify it.
回答 2
>>> x = [0, 1, 2, 3, 4]
>>> x.pop(0)
0
更多关于此这里。
>>> x = [0, 1, 2, 3, 4]
>>> x.pop(0)
0
More on this here.
回答 3
使用列表切片,请参阅有关列表的Python教程以获取更多详细信息:
>>> l = [0, 1, 2, 3, 4]
>>> l[1:]
[1, 2, 3, 4]
With list slicing, see the Python tutorial about lists for more details:
>>> l = [0, 1, 2, 3, 4]
>>> l[1:]
[1, 2, 3, 4]
回答 4
你会这样做
l = [0, 1, 2, 3, 4]
l.pop(0)
要么 l = l[1:]
利弊
使用pop可以获取值
说x = l.pop(0)
x
会0
you would just do this
l = [0, 1, 2, 3, 4]
l.pop(0)
or l = l[1:]
Pros and Cons
Using pop you can retrieve the value
say x = l.pop(0)
x
would be 0
回答 5
然后将其删除:
x = [0, 1, 2, 3, 4]
del x[0]
print x
# [1, 2, 3, 4]
Then just delete it:
x = [0, 1, 2, 3, 4]
del x[0]
print x
# [1, 2, 3, 4]
回答 6
您可以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 use list.reverse()
to reverse the list, then list.pop()
to remove the last element, for example:
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
回答 7
您也可以使用list.remove(a[0])
来pop
删除列表中的第一个元素。
>>>> a=[1,2,3,4,5]
>>>> a.remove(a[0])
>>>> print a
>>>> [2,3,4,5]
You can also use list.remove(a[0])
to pop
out the first element in the list.
>>>> a=[1,2,3,4,5]
>>>> a.remove(a[0])
>>>> print a
>>>> [2,3,4,5]
回答 8
如果使用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]
If you are working with numpy you need to use the delete method:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
a = np.delete(a, 0)
print(a) # [2 3 4 5]
回答 9
有一个称为“双端队列”或双头队列的数据结构,它比列表更快,更高效。您可以使用列表并将其转换为双端队列,并在其中进行所需的转换。您也可以将双端队列转换回列表。
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还提供了非常有用的功能,例如将元素插入列表的任一侧或任何特定的索引。您也可以旋转或反转双端队列。试试看!!
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.
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!!