是否有附加和插入都存在的原因? [英] Is there a reason why append and insert are both there?

查看:23
本文介绍了是否有附加和插入都存在的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我肯定不是我想成为的 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 方法,该方法通常会在线性时间内运行,除非未指定参数,在这种情况下它将以恒定时间(非常奇怪)运行,第二种方法添加了总是恒定时间.这种类型的设计决策可以在 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.

这篇关于是否有附加和插入都存在的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆