append 和 insert 都存在有什么原因吗?

2022-01-22 00:00:00 python append insert

问题描述

我肯定不是我想成为的 Python 大师,而且我主要在业余时间学习学习/实验,我很可能会为有经验的用户提出一个微不足道的问题......然而,我真的很想了解,这是一个对我有很大帮助的地方.

I'm surely not the Python guru I'd like to be and I mostly learn studying/experimenting in my spare time, it is very likely I'm going to make a trivial question for experienced users... yet, I really want to understand and this is a place that helps me a lot.

现在,在适当的前提下,Python 文档说:

Now, after the due premise, Python documentation says:

4.6.3.可变序列类型

4.6.3. Mutable Sequence Types

s.append(x) 将 x 附加到序列的末尾(与s[len(s):len(s)] = [x])

s.append(x) appends x to the end of the sequence (same as s[len(s):len(s)] = [x])

[...]

s.insert(i, x) 在 i 给定的索引处将 x 插入到 s 中(与s[i:i] = [x])

s.insert(i, x) inserts x into s at the index given by i (same as s[i:i] = [x])

此外:

5.1.更多关于列表

list.append(x) 将一个项目添加到列表的末尾.相当于a[len(a):] = [x].

list.append(x) Add an item to the end of the list. Equivalent to a[len(a):] = [x].

[...]

list.insert(i, x) 在给定位置插入一个项目.首先参数是要插入的元素的索引,所以a.insert(0, x) 在列表的前面插入,a.insert(len(a),x) 等价于 a.append(x).

list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

所以现在我想为什么有两种方法可以做,基本上是同一件事?难道只有一个 append/insert(x, i=len(this)) 是可能的(并且更简单),其中 i 本来是一个可选参数并且,当不存在时,是否意味着添加到列表的末尾?

So now I'm wondering why there are two methods to do, basically, the same thing? Wouldn't it been possible (and simpler) to have just one append/insert(x, i=len(this)) where the i would have been an optional parameter and, when not present, would have meant add to the end of the list?


解决方案

这里append和insert的区别和正常用法一样,在大多数文本编辑器中都是一样的.Append 添加到 列表的末尾,而 insert 添加 在指定索引的前面.它们是不同方法的原因既是因为它们做不同的事情,而且因为 append 可以预期是一个快速操作,而 insert 可能需要一段时间,具体取决于大小列表和您要插入的位置,因为插入点之后的所有内容都必须重新索引.

The difference between append and insert here is the same as in normal usage, and in most text editors. Append adds to the end of the list, while insert adds in front of a specified index. The reason they are different methods is both because they do different things, and because append can be expected to be a quick operation, while insert might take a while depending on the size of the list and where you're inserting, because everything after the insertion point has to be reindexed.

我不知道 insertappend 采用不同方法的实际原因,但我会做出有根据的猜测,它有助于提醒开发人员固有的性能差异.而不是一个带有可选参数的 insert 方法,它通常会在线性时间运行,除非没有指定参数,在这种情况下它将以恒定时间运行(非常奇怪),第二种方法添加了 always 恒定时间.这种类型的设计决策可以在 Python 的其他地方看到,例如当像 list.sort 这样的方法返回 None 而不是一个新列表时,作为 提醒它们是就地操作,而不是创建(和返回)新列表.

I'm not privy to the actual reasons insert and append were made different methods, but I would make an educated guess that it is to help remind the developer of the inherent performance difference. Rather than one insert method, with an optional parameter, which would normally run in linear time except when the parameter was not specified, in which case it would run in constant time (very odd), a second method which would always be constant time was added. This type of design decision can be seen in other places in Python, such as when methods like list.sort return None, instead of a new list, as a reminder that they are in-place operations, and not creating (and returning) a new list.

相关文章