Python的insert()方法与切片插入的实现和性能区别

Implementation and performance difference between Python#39;s insert() method and inserting by slicing(Python的insert()方法与切片插入的实现和性能区别)
本文介绍了Python的insert()方法与切片插入的实现和性能区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

以下方式在python列表中插入元素有什么区别?

What is the difference between inserting an element in a python list in the following ways?

myList.insert(at, myValue)
myList[at:at] = [myValue]

我进行了一些测试,两者的性能非常相似,但切片插入始终会产生稍好的结果.我的问题是关于实现和性能的差异,而不是行为.

I have run some tests and the performance of the two are very similar, but the slicing insert consistently produces slightly better results. My question is regarding the difference in implementation and performance, not the behaviour.

推荐答案

我们有相同的行为,见下图:

We have the same behaviour, see bellow:

默认行为是在给定索引处插入项目;更大索引处的每个值都被移到最后一个位置.

The default behaviour is to insert the item at the given index; each value at greater index are shifted one position to the end.

>>> my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> my_list.insert(5, 'item')
>>> my_list
['a', 'b', 'c', 'd', 'e', 'item', 'f', 'g']

>>> my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> my_list.insert(-3, 'item')
>>> my_list
['a', 'b', 'c', 'd', 'item', 'e', 'f', 'g']

如果列表为空,则正常追加该项目.

If the list is empty, the item is appended normally.

>>> my_list = []
>>> my_list.insert(5, 'item')
>>> my_list
['item']

>>> my_list = []
>>> my_list.insert(-3, 'item')
>>> my_list
['item']

如果索引超出范围,则如果索引为正,则将项目附加到末尾,如果索引为负,则将项目附加到开头.不会引发异常.

If the index is out of bounds, the item is appended to the end if the index is positive or to the beginning if negative. No exception is raised.

>>> my_list = ['a', 'b']
>>> my_list.insert(5, 'item')
>>> my_list
['a', 'b', 'item']

>>> my_list = ['a', 'b']
>>> my_list.insert(-3, 'item')
>>> my_list
['item', 'a', 'b']

切片符号的行为完全相同,在一系列相同索引的情况下:

We have exactly the same behaviour with slice notation, in the case of a range of same indexes:

>>> my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> my_list[5:5] = ['item']
>>> my_list
['a', 'b', 'c', 'd', 'e', 'item', 'f', 'g']

>>> my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> my_list[-3:-3] = ['item']
>>> my_list
['a', 'b', 'c', 'd', 'item', 'e', 'f', 'g']

>>> my_list = []
>>> my_list[5:5] = ['item']
>>> my_list
['item']

>>> my_list = []
>>> my_list[-3:-3] = ['item']
>>> my_list
['item']

>>> my_list = ['a', 'b']
>>> my_list[5:5] = ['item']
>>> my_list
['a', 'b', 'item']

>>> my_list = ['a', 'b']
>>> my_list[-3:-3] = ['item']
>>> my_list
['item', 'a', 'b']

切片表示法与使用 slice 对象调用 __setitem__() 方法相同:

The slice notation is the same as calling __setitem__() method with a slice object:

>>> my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> my_list.__setitem__(slice(5, 5), ['item'])
>>> my_list
['a', 'b', 'c', 'd', 'e', 'item', 'f', 'g']

这篇关于Python的insert()方法与切片插入的实现和性能区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)