我有这个:
>>> a = [1, 2, 4]
>>> print a
[1, 2, 4]
>>> print a.insert(2, 3)
None
>>> print a
[1, 2, 3, 4]
>>> b = a.insert(3, 6)
>>> print b
None
>>> print a
[1, 2, 3, 6, 4]
有没有办法我可以得到更新的列表作为结果,而不是更新原始列表?
我有这个:
>>> a = [1, 2, 4]
>>> print a
[1, 2, 4]
>>> print a.insert(2, 3)
None
>>> print a
[1, 2, 3, 4]
>>> b = a.insert(3, 6)
>>> print b
None
>>> print a
[1, 2, 3, 6, 4]
有没有办法我可以得到更新的列表作为结果,而不是更新原始列表?
l.insert(index, obj)
实际上并没有返回任何东西。它只是更新列表。
正如 ATO 所说,你可以做到b = a[:index] + [obj] + a[index:]
。但是,另一种方法是:
a = [1, 2, 4]
b = a[:]
b.insert(2, 3)
您还可以使用列表中的切片索引插入元素。例如:
>>> a = [1, 2, 4]
>>> insert_at = 2 # Index at which you want to insert item
>>> b = a[:] # Created copy of list "a" as "b".
# Skip this step if you are ok with modifying the original list
>>> b[insert_at:insert_at] = [3] # Insert "3" within "b"
>>> b
[1, 2, 3, 4]
为了在给定的索引处插入多个元素,您需要做的就是使用list
要插入的多个元素中的一个。例如:
>>> a = [1, 2, 4]
>>> insert_at = 2 # Index starting from which multiple elements will be inserted
# List of elements that you want to insert together at "index_at" (above) position
>>> insert_elements = [3, 5, 6]
>>> a[insert_at:insert_at] = insert_elements
>>> a # [3, 5, 6] are inserted together in `a` starting at index "2"
[1, 2, 3, 5, 6, 4]
要了解更多关于切片索引的信息,可以参考:理解切片表示法。
注意:在 Python 3.x 中,切片索引和切片索引之间的性能差异list.index(...)
显着降低,并且两者几乎相等。但是,在 Python 2.x 中,这种差异非常明显。我在这个答案后面分享了性能比较。
使用列表理解的替代方案 (但在性能方面非常慢):
作为替代方案,它也可以使用列表推导来实现enumerate
。(但请不要这样做。这只是为了说明):
>>> a = [1, 2, 4]
>>> insert_at = 2
>>> b = [y for i, x in enumerate(a) for y in ((3, x) if i == insert_at else (x, ))]
>>> b
[1, 2, 3, 4]
这timeit
是所有答案与 Python 3.9.1 和 Python 2.7.16 上的 1000 个元素列表的比较。答案按两个 Python 版本的性能顺序列出。
我使用切片插入的答案- 最快(每循环 2.25 微秒)
python3 -m timeit -s "a = list(range(1000))" "b = a[:]; b[500:500] = [3]"
100000 loops, best of 5: 2.25 µsec per loop
Rushy Panchal 的回答最多票数使用list.insert(...)
- Second (2.33 µsec per loop)
python3 -m timeit -s "a = list(range(1000))" "b = a[:]; b.insert(500, 3)"
100000 loops, best of 5: 2.33 µsec per loop
ATOzTOA 接受的基于切片列表合并的答案 - 第三(每个循环 5.01 微秒)
python3 -m timeit -s "a = list(range(1000))" "b = a[:500] + [3] + a[500:]"
50000 loops, best of 5: 5.01 µsec per loop
我对列表理解的回答和enumerate
- 第四(非常慢,每个循环 135 微秒)
python3 -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]"
2000 loops, best of 5: 135 µsec per loop
我使用切片插入的答案- 最快(每循环 2.09 微秒)
python -m timeit -s "a = list(range(1000))" "b = a[:]; b[500:500] = [3]"
100000 loops, best of 3: 2.09 µsec per loop
Rushy Panchal 的回答最多票数使用list.insert(...)
- Second (2.36 µsec per loop)
python -m timeit -s "a = list(range(1000))" "b = a[:]; b.insert(500, 3)"
100000 loops, best of 3: 2.36 µsec per loop
ATOzTOA 接受的基于切片列表合并的答案 - 第三(每个循环 4.44 微秒)
python -m timeit -s "a = list(range(1000))" "b = a[:500] + [3] + a[500:]"
100000 loops, best of 3: 4.44 µsec per loop
我对列表理解的回答和enumerate
- 第四(非常慢,每个循环 103 微秒)
python -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]"
10000 loops, best of 3: 103 µsec per loop
我得到的最短的:b = a[:2] + [3] + a[2:]
>>>
>>> a = [1, 2, 4]
>>> print a
[1, 2, 4]
>>> b = a[:2] + [3] + a[2:]
>>> print a
[1, 2, 4]
>>> print b
[1, 2, 3, 4]
最干净的方法是复制列表,然后将对象插入到副本中。在 Python 3 上,这可以通过以下方式完成list.copy
:
new = old.copy()
new.insert(index, value)
在 Python 2 上,可以通过以下方式复制列表new = old[:]
(这也适用于 Python 3)。
在性能方面,与其他提议的方法没有区别:
$ python --version
Python 3.8.1
$ python -m timeit -s "a = list(range(1000))" "b = a.copy(); b.insert(500, 3)"
100000 loops, best of 5: 2.84 µsec per loop
$ python -m timeit -s "a = list(range(1000))" "b = a.copy(); b[500:500] = (3,)"
100000 loops, best of 5: 2.76 µsec per loop
这是添加单个项目的方法,特定索引连接列表中的单个项目与另一个列表
>>> expences = [2200, 2350, 2600, 2130, 2190]
>>> expences.append(1980)
>>> expences
[2200, 2350, 2600, 2130, 2190, 1980]
>>> expences.insert(1, 1200)
>>> expences
[2200, 1200, 2350, 2600, 2130, 2190, 1980]
>>> newElm = [2550, 2123, 2430]
>>> expences.extend(newElm)
>>> expences
[2200, 1200, 2350, 2600, 2130, 2190, 1980, 2550, 2123, 2430]
>>>
使用Python 列表 insert() 方法。用法:
#句法
insert() 方法的语法 -</p>
list.insert(index, obj)
#参数
#Return Value 此方法不返回任何值,但它在给定索引处插入给定元素。
例子:
a = [1,2,4,5]
a.insert(2,3)
print(a)
退货[1, 2, 3, 4, 5]