简体   繁体   English

为什么 Python 的列表没有 shift/unshift 方法?

[英]Why Python's list does not have shift/unshift methods?

I am wondering why the default list in Python does not have any shift , unshift methods.我想知道为什么 Python 中的默认list没有任何shiftunshift方法。 Maybe there is an obvious reason for it like the way the lists are ordered in memory.也许有一个明显的原因,比如列表在内存中的排序方式。

So currently, I know that I can use append to add an item at the end of a list and pop to remove an element from the end.所以目前,我知道我可以使用append在列表的末尾添加一个项目,并使用pop从末尾删除一个元素。 However, I can only use list concatenation to imitate the behavior of a missing shift or unshift method.但是,我只能使用列表连接来模仿缺少shiftunshift方法的行为。

>>> a = [1,2,3,4,5]
>>> a = [0] + a  # Unshift / Push
>>> a
[0,1,2,3,4,5]
>>> a = a[1:]  # Shift / UnPush
>>> a
[1,2,3,4,5]

Did I miss something?我错过了什么?

Python lists were optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation. Python 列表针对快速固定长度操作进行了优化,并且会为pop(0)insert(0, v)操作带来 O(n) 内存移动成本,这会改变底层数据表示的大小和位置。 Actually, the "list" datatype in CPython works differently as to what many other languages might call a list (eg a linked-list) - it is implemented more similarly to what other languages might call an array , though there are some differences here too.实际上,CPython 中的“列表”数据类型与许多其他语言可能称之为列表(例如链表)的工作方式不同 - 它的实现与其他语言可能称之为数组的内容更相似,尽管这里也有一些差异.

You may be interested instead in collections.deque , which is a list-like container with fast appends and pops on either end.您可能对collections.deque感兴趣,它是一个类似列表的容器,两端都有快速追加和弹出。

Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.双端队列支持线程安全、内存高效的追加和从双端队列的任一侧弹出,在任一方向上的 O(1) 性能大致相同。

The missing methods you appear to be asking about are provided under the names appendleft and popleft :您似乎在询问的缺失方法在名称appendleftpopleft下提供:

appendleft ( x )追加左( x )

Add x to the left side of the deque.将 x 添加到双端队列的左侧。

popleft ()弹出左键()

Remove and return an element from the left side of the deque.从双端队列的左侧移除并返回一个元素。 If no elements are present, raises an IndexError.如果不存在元素,则引发 IndexError。

Of course there is a trade-off, and indexing or inserting/removing near the middle of the deque is slow.当然有一个权衡,在双端队列中间附近建立索引或插入/删除很慢。 In fact deque.insert(index, object) wasn't even possible before Python 3.5 , you would need to rotate, insert/pop, and rotate back.事实上deque.insert(index, object)在 Python 3.5 之前甚至是不可能的,你需要旋转、插入/弹出和旋转回来。 You also lose slicing, so if you needed that you'll have to write something annoying with eg itertools.islice instead.你也失去了切片,所以如果你需要,你必须写一些烦人的东西,例如itertools.islice

For further discussion of the advantages and disadvantages of deque vs list data structures, see How are deques in Python implemented, and when are they worse than lists?要进一步讨论dequelist数据结构的优缺点,请参阅Python 中deque是如何实现的,它们何时比列表更糟糕?

in Python3在 Python3 中

we have insert method on a list.我们在列表上有插入方法。 takes the value you require to add and also the index where you want to add it.获取您需要添加的值以及要添加它的索引。

arrayOrList  = [1,2,3,4,5]
arrayOrList.insert(0 , 0) 
print(arrayOrList)

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

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