在python中合并两个列表的最快方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17044508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the fastest way to merge two lists in python?
提问by Naffi
Given,
鉴于,
list_1 = [1,2,3,4]
list_2 = [5,6,7,8]
What is the fastest wayto achieve the following in python?
在python中实现以下目标的最快方法是什么?
list = [1,2,3,4,5,6,7,8]
Please note that, there can be many ways to merge two lists in python. I am looking for the most time efficient way.
请注意,在 python 中可以有多种方法来合并两个列表。我正在寻找最省时的方式。
[EDIT]++++++++++++++++++++++++++++++++++++++++++++[EDIT]
[编辑]++++++++++++++++++++++++++++++++++++++++++++[编辑]
Thanks for all the answers. Getting your ideas, I tried the following and here is my understanding.
感谢所有的答案。得到你的想法,我尝试了以下方法,这是我的理解。
CODE
代码
import time
c = list(range(1,10000000))
c_n = list(range(10000000, 20000000))
start = time.time()
c = c+c_n
print len(c)
print time.time() - start
c = list(range(1,10000000))
start = time.time()
for i in c_n:
c.append(i)
print len(c)
print time.time() - start
c = list(range(1,10000000))
start = time.time()
c.extend(c_n)
print len(c)
print time.time() - start
OUTPUT
输出
19999999
0.125061035156
19999999
1.02858018875
19999999
0.03928399086
So, if someone does not bother reusing list_1/list_2 in the question then extendis the way to go. On the other hand, "+"is the fastest way.
因此,如果有人不想在问题中重用 list_1/list_2,那么扩展就是要走的路。另一方面,“+”是最快的方式。
I am not sure about other options though.
我不确定其他选择。
Thanks again :-)
再次感谢 :-)
回答by Sukrit Kalra
list_1 + list_2does it. Example -
list_1 + list_2可以。例子 -
>>> list_1 = [1,2,3,4]
>>> list_2 = [5,6,7,8]
>>> list_1 + list_2
[1, 2, 3, 4, 5, 6, 7, 8]
回答by phant0m
You can just use concatenation:
您可以只使用串联:
list = list_1 + list_2
If you don't need to keep list_1 around, you can just modify it:
如果你不需要保留 list_1 ,你可以修改它:
list_1.extend(list_2)
回答by Mohit Solanki
If you are using python 3, there is one more way to do this and a little bit faster (tested only on python 3.7)
如果您使用的是 python 3,还有一种方法可以做到这一点,而且速度要快一些(仅在 python 3.7 上测试)
[*list1, *list2]
Benchmark
基准
from timeit import timeit
x = list(range(10000))
y = list(x)
def one():
x + y
def two():
[*x, *y]
print(timeit(one, number=1000, globals={'x':x, 'y': y}))
print(timeit(two, number=1000, globals={'x':x, 'y': y}))
0.10456193100253586
0.09631731400440913
回答by Kiran Prasad
I tested out several ways to merge two lists (see below) and came up with the following order after running each several times to normalize the cache changes (which make about a 15% difference).
我测试了几种合并两个列表的方法(见下文),并在每次运行几次后得出以下顺序以规范缓存更改(大约有 15% 的差异)。
import time
c = list(range(1,10000000))
c_n = list(range(10000000, 20000000))
start = time.time()
*insert method here*
print (time.time()-start)
Method 1:
c.extend(c_n)- Representative result: 0.11861872673034668
Method 2:
c += c_n- Representative result: 0.10558319091796875
Method 3:
c = c + c_n- Representative result: 0.25804924964904785
Method 4:
c = [*c, *c_n]- Representative result: 0.22019600868225098
方法一:
c.extend(c_n)- 代表结果:0.11861872673034668
方法二:
c += c_n- 代表结果:0.10558319091796875
方法三:
c = c + c_n- 代表结果:0.25804924964904785
方法四:
c = [*c, *c_n]- 代表性结果:0.22019600868225098
ConclusionUse +=or .extend()if you want to merge in place. They are significantly faster.
结论使用+=或.extend()如果您想就地合并。它们的速度要快得多。

