python列表连接效率 [英] python list concatenation efficiency
本文介绍了python列表连接效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下情况下,连接两个列表list_a
和list_b
的最有效方法是什么?
What is the most efficient way to concatenate two lists list_a
and list_b
when:
-
list_b
个项目必须放在list_a
个项目之前 - 结果必须放在
list_a
中
list_b
items have to be placed beforelist_a
items- the result must be placed in
list_a
我有4种可能:
# 1
list_a = list_b + list_a
# 2
for item in list_b:
list_a.insert(0, item)
# 3
for item in self.list_a:
list_b.append(item)
list_a = list_b
# 4
list_a[0:0] = list_b
谢谢!
推荐答案
以下是有关 BigYellowCactus 随着列表长度的增加而发展.垂直轴是在usec中初始化两个列表并将一个列表插入另一个列表所需的时间.横轴是列表中的项目数.
Here's a graph of how the timings used in the answer of BigYellowCactus develop as the length of the lists increase. The vertical axis is the time required to initialize both lists and insert one in front of the other, in usec. The horizontal axis is the number of items in the lists.
这篇关于python列表连接效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文