小编Naf*_*ffi的帖子

在python中合并两个列表的最快方法是什么?

鉴于,

list_1 = [1,2,3,4]
list_2 = [5,6,7,8]
Run Code Online (Sandbox Code Playgroud)

在python中实现以下功能的最快方法是什么?

list = [1,2,3,4,5,6,7,8]
Run Code Online (Sandbox Code Playgroud)

请注意,可以有很多方法在python中合并两个列表.我正在寻找最有效率的方式.

[编辑] ++++++++++++++++++++++++++++++++++++++++++++ [编辑]

感谢所有的答案.得到你的想法,我尝试了以下,这是我的理解.

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
Run Code Online (Sandbox Code Playgroud)

OUTPUT

19999999
0.125061035156
19999999
1.02858018875
19999999
0.03928399086
Run Code Online (Sandbox Code Playgroud)

所以,如果有人不打算在问题中重复使用list_1/list_2,那么延伸就是要走的路.另一方面, …

python performance merge list

22
推荐指数
4
解决办法
5万
查看次数

标签 统计

list ×1

merge ×1

performance ×1

python ×1