问题:为什么python列表具有pop()但没有push()

有谁知道为什么list.append不调用Python的函数list.push,因为已经有一个list.pop删除并返回最后一个元素(索引为-1)并且list.append语义与该用法一致的原因?

Does anyone know why Python’s list.append function is not called list.push given that there’s already a list.pop that removes and returns the last element (that indexed at -1) and list.append semantic is consistent with that use?


回答 0

因为“ append”早在想到“ pop”之前就已经存在。受Python 0.9.1支持的list.append于1991年初。通过比较,这是在comp.lang.python上讨论的有关在1997年添加pop 的一部分。Guido 写道:

为了实现一个堆栈,需要添加一个list.pop()原语(不,基于任何原则,我都不反对这种特定的堆栈)。可以添加list.push()使其与list.pop()对称,但是我不太喜欢针对同一操作使用多个名称-早晚要读取使用另一个名称的代码,所以您需要同时学习两者,这会增加认知负担。

您还可以看到他讨论了是否应该在元素[0]或元素[-1]之后或在元素[-1]之后发布推/弹出/放/拉的想法:他在其中发布了对Icon列表的引用:

我仍然认为最好将所有这些都排除在列表对象实现之外-如果您需要具有特定语义的堆栈或队列,请编写一个使用列表的小类

换句话说,对于直接实现为Python列表的堆栈,该堆栈已经支持快速的append()和del list [-1],默认情况下list.pop()在最后一个元素上起作用是有意义的。即使其他语言做了不同的处理。

这里的隐含含义是,大多数人都需要追加到列表,但是很少有人有机会将列表视为堆栈,这就是为什么list.append出现得这么早的原因。

Because “append” existed long before “pop” was thought of. Python 0.9.1 supported list.append in early 1991. By comparison, here’s part of a discussion on comp.lang.python about adding pop in 1997. Guido wrote:

To implement a stack, one would need to add a list.pop() primitive (and no, I’m not against this particular one on the basis of any principle). list.push() could be added for symmetry with list.pop() but I’m not a big fan of multiple names for the same operation — sooner or later you’re going to read code that uses the other one, so you need to learn both, which is more cognitive load.

You can also see he discusses the idea of if push/pop/put/pull should be at element [0] or after element [-1] where he posts a reference to Icon’s list:

I stil think that all this is best left out of the list object implementation — if you need a stack, or a queue, with particular semantics, write a little class that uses a lists

In other words, for stacks implemented directly as Python lists, which already supports fast append(), and del list[-1], it makes sense that list.pop() work by default on the last element. Even if other languages do it differently.

Implicit here is that most people need to append to a list, but many fewer have occasion to treat lists as stacks, which is why list.append came in so much earlier.


回答 1

因为它附加;它不会推动。“添加”添加到列表的末尾,“推”添加到列表的前部。

想一想队列还是堆栈。

http://docs.python.org/tutorial/datastructures.html

编辑:为了更准确地改写我的第二句话,“追加”非常清楚地意味着在列表的末尾添加一些内容,而不管其基础实现如何。当一个新元素被“推”到什么地方时,还不清楚。压入堆栈是将某些东西放在“顶部”,但是其在底层数据结构中的实际位置完全取决于实现。另一方面,推入队列意味着将其添加到末尾。

Because it appends; it doesn’t push. “Appending” adds to the end of a list, “pushing” adds to the front.

Think of a queue vs. a stack.

http://docs.python.org/tutorial/datastructures.html

Edit: To reword my second sentence more exactly, “Appending” very clearly implies adding something to the end of a list, regardless of the underlying implementation. Where a new element gets added when it’s “pushed” is less clear. Pushing onto a stack is putting something on “top,” but where it actually goes in the underlying data structure completely depends on implementation. On the other hand, pushing onto a queue implies adding it to the end.


回答 2

因为它将元素添加到列表?推栈通常在引用堆栈时使用。

Because it appends an element to a list? Push is usually used when referring to stacks.


回答 3

因为“添加”在直觉上意味着“在列表末尾添加”。如果它被称为“ push”,那么我们是否要在列表的末尾还是顶部添加内容尚不清楚。

Because “append” intuitively means “add at the end of the list”. If it was called “push”, then it would be unclear whether we’re adding stuff at the tail or at head of the list.


回答 4

无论如何,这都不是正式的答案(只是基于使用该语言的猜测),但是Python允许您将列表用作堆栈(例如,本教程的5.1.1节)。但是,列表仍然是列表的第一位,因此,这两个共同的操作都使用列表项(即追加),而不是堆栈项(即推入)。由于弹出操作在列表中并不常见(尽管可以使用’removeLast’),所以他们定义了pop()而不是push()。

Not an official answer by any means (just a guess based on using the language), but Python allows you to use lists as stacks (e.g., section 5.1.1 of the tutorial). However, a list is still first of all a list, so the operations that are common to both use list terms (i.e., append) rather than stack terms (i.e., push). Since a pop operation isn’t that common in lists (though ‘removeLast’ could have been used), they defined a pop() but not a push().


回答 5

好的,这里有个人意见,但“追加”和“前置”表示集合中的精确位置。

Push和Pop确实是可以应用于集合两端的概念…只要您保持一致…出于某种原因,对我来说,Push()似乎应该应用于集合的前端组…

Ok, personal opinion here, but Append and Prepend imply precise positions in a set.

Push and Pop are really concepts that can be applied to either end of a set… Just as long as you’re consistent… For some reason, to me, Push() seems like it should apply to the front of a set…


回答 6

仅供参考,创建具有push方法的列表并不困难:

>>> class StackList(list):
...     def push(self, item):
...             self.append(item)
... 
>>> x = StackList([1,2,3])
>>> x
[1, 2, 3]
>>> x.push(4)
>>> x
[1, 2, 3, 4]

堆栈是某种抽象的数据类型。“推”和“弹出”的思想在很大程度上与堆栈的实际实现方式无关。例如,理论上您可以实现这样的堆栈(尽管我不知道为什么会这样):

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

…而且我还没有开始使用链表来实现堆栈。

FYI, it’s not terribly difficult to make a list that has a push method:

>>> class StackList(list):
...     def push(self, item):
...             self.append(item)
... 
>>> x = StackList([1,2,3])
>>> x
[1, 2, 3]
>>> x.push(4)
>>> x
[1, 2, 3, 4]

A stack is a somewhat abstract datatype. The idea of “pushing” and “popping” are largely independent of how the stack is actually implemented. For example, you could theoretically implement a stack like this (although I don’t know why you would):

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

…and I haven’t gotten into using linked lists to implement a stack.


回答 7

推送是定义的 堆栈行为;如果将A推入堆栈(B,C,D),您将得到(A,B,C,D)。

如果您使用python append,则结果数据集将看起来像(B,C,D,A)

编辑:哇,圣ped。

我认为从我的示例中可以清楚看出列表的哪一部分是顶部,哪一部分是底部。假设我们大多数人从左到右阅读,则任何列表的第一个元素始终将位于左侧。

Push is a defined stack behaviour; if you pushed A on to stack (B,C,D) you would get (A,B,C,D).

If you used python append, the resulting dataset would look like (B,C,D,A)

Edit: Wow, holy pedantry.

I would assume that it would be clear from my example which part of the list is the top, and which part is the bottom. Assuming that most of us here read from left to right, the first element of any list is always going to be on the left.


回答 8

可能是因为Python(C Python)的原始版本是用C而不是C ++编写的。

通过将事物推入事物的背面来形成列表的想法可能不如附加它们的想法众所周知。

Probably because the original version of Python (CPython) was written in C, not C++.

The idea that a list is formed by pushing things onto the back of something is probably not as well-known as the thought of appending them.


回答 9

就自助餐厅或自助餐中一叠盘子或盘子的隐喻而言,“推”和“弹出”是有意义的,特别是在下方带有弹簧的顶托类型的托座(理论上或多或少…)在同一地方,无论下面有多少板。

如果卸下托盘,则弹簧上的重量会减小一点,并且纸叠会“弹起”一点,如果放回盘子,它会“压下”纸叠。因此,如果您将列表视为堆栈,而将最后一个元素视为顶部,那么您就不会感到太多困惑。

Push and Pop make sense in terms of the metaphor of a stack of plates or trays in a cafeteria or buffet, specifically the ones in type of holder that has a spring underneath so the top plate is (more or less… in theory) in the same place no matter how many plates are under it.

If you remove a tray, the weight on the spring is a little less and the stack “pops” up a little, if you put the plate back, it “push”es the stack down. So if you think about the list as a stack and the last element as being on top, then you shouldn’t have much confusion.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。