python - 在列表之间插入的最有效方法?

标签 python list list-manipulation

我正在读取一个文件并构建一个列表 a2。 我想在前两项之后插入 3 行以从列表 b 中列出 a2

b = ["This is a line", "another line", "and another one"]
a2 = ['a1', 'a2', 'a3']

i = 0
for x, y in map(None, a2[0:2], a2):
    i = i + 1
    if x == y:
        continue
    else:
        for newLine in b:
            a2.insert(i-1, newLine)
            i = i+1
print a2

上面确实给了我预期的结果,比如 ['a1', 'a2', 'This is a line', 'another line', 'and another one', 'a3'] 但是因为我要用巨大的文本文件构建列表并在它们之间插入几行,所以我想我必须使它的性能更直观!

最佳答案

怎么样-

a2[2:2] = b

演示 -

>>> b = ["This is a line", "another line", "and another one"]
>>> a2 = ['a1', 'a2', 'a3']
>>> a2[2:2] = b
>>> a2
['a1', 'a2', 'This is a line', 'another line', 'and another one', 'a3']

关于我所知道的一些方法的时间信息(包括 OP 发布的方法)-

def func1():
    b = ["This is a line", "another line", "and another one"]
    a2 = ['a1', 'a2', 'a3']
    i = 0
    for x, y in map(None, a2[0:2], a2):
        i = i + 1
        if x == y:
            continue
        else:
            for newLine in b:
                a2.insert(i-1, newLine)
                i = i+1
    return a2


def func2():
    b = ["This is a line", "another line", "and another one"]
    a2 = ['a1', 'a2', 'a3']
    a2 = a2[:2] + b + a2[2:]
    return a2

def func3():
    b = ["This is a line", "another line", "and another one"]
    a2 = ['a1', 'a2', 'a3']
    a2[2:2] = b
    return a2


import timeit

print timeit.timeit(func1,number=500000)
print timeit.timeit(func2,number=500000)
print timeit.timeit(func3,number=500000)

结果-

1.81288409233
0.621006011963
0.341125011444

a有100000个元素和b有1000个元素的计时结果-

def func1():
    global a2
    global b
    i = 0
    for x, y in map(None, a2[0:2], a2):
        i = i + 1
        if x == y:
            continue
        else:
            for newLine in b:
                a2.insert(i-1, newLine)
                i = i+1
            break
    return a2


def func2():
    global a2
    global b
    a2 = a2[:2] + b + a2[2:]
    return a2

def func3():
    global a2
    global b
    a2[2:2] = b
    return a2

def func4():
    global a2
    global b
    a2.reverse()
    b.reverse()
    for i in b:
        a2.insert(-2, i)
    return a2

import timeit

a2 = ['a1' for _ in range(100000)]
b = ['a2' for i in range(1000)]

print timeit.timeit(func1,number=10,setup = 'from __main__ import a2,b')
print timeit.timeit(func2,number=10,setup = 'from __main__ import a2,b')
print timeit.timeit(func3,number=10,setup = 'from __main__ import a2,b')
print timeit.timeit(func4,number=10,setup = 'from __main__ import a2,b')

结果-

1.00535297394
0.0210499763489
0.001296043396
0.0044310092926

引用计时测试 - https://ideone.com/k4DANI

关于python - 在列表之间插入的最有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31624213/

相关文章:

r - 根据其他向量在列中查找匹配值

wolfram-mathematica - Mathematica 列表的最大值

python - 使用 pytesseract 时如何设置配置 load_system_dawg 以改善结果?

C#:将简单结构列表转换为 byte[]

python gtk 3.0 是否不支持从辅助线程访问 GUI?

python - 使用 Python 和 split() 解析文本文件

python - 为什么复制的字典指向同一个目录而列表却没有?

python - 访问 split 函数后的最后一个字符串以创建新列表

python - 如何在 matplotlib 中调整 x 轴

python - Django 开发服务器可以正确地为 SVG 服务吗?