是否有一个原因,追加和插入都在那里? [英] Is there a reason why append and insert are both there?

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

问题描述

我肯定不是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.

现在,到期premise后,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)x插入到s由我给定的索引(同
  S [I:我] = [X])

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

,此外

5.1。更多关于列表

5.1. More on Lists

list.append(x)的一个项目添加到列表的末尾。相当于
   一个[长度(一):] = [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).

所以现在我想知道的为什么有两种方法做的,基本上,同样的事情的?那岂不是成为可能(和更简单),以只有一个追加/插入(X,I = LEN(本))其中 I 将是一个可选参数,当没有present,就意味着添加到列表中?

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?

推荐答案

追加之间的差别,并插入这里是一样正常使用,并且在大多数文本编辑器。追加增加的列表的末尾的,而插入件增加的在一个指定的索引的前面。它们是不同的方法的原因是因为一来他们做不同的事情,因为追加可以预期是一个快速的操作,而插入可能需要一段时间取决于列表,并在那里你要插入的大小,因为插入点后一切都要重新索引。

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.

我并不了解实际的原因插入追加作了不同的方法,但我会做一个受过教育的猜测,这是帮助提醒固有的性能差异的开发商。而不是一个插入法,用一个可选的参数,这通常会以线性时间运行时不指定参数,所不同的,在这种情况下,它会在固定时间内运行(很奇),加入该会的总是的恒定时间的第二种方法。这种类型的设计决策可以在Python中的其他地方,如可以看出,当像 list.sort 收益方法,而不是一个新的列表,作为一个的提醒的,他们是就地操作,而不是创建(和返回)一个新的列表。

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天全站免登陆