Python 如何将两个列表合并为一个列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3471999/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:14:23  来源:igfitidea点击:

How do I merge two lists into a single list?

python

提问by sureshvv

I have

我有

a = [1, 2]
b = ['a', 'b']

I want

我想要

c = [1, 'a', 2, 'b']

采纳答案by John La Rooy

[j for i in zip(a,b) for j in i]

回答by Mark Byers

If the order of the elements much match the order in your example then you can use a combination of zipand chain:

如果元素的顺序与您的示例中的顺序非常匹配,那么您可以使用zipchain的组合:

from itertools import chain
c = list(chain(*zip(a,b)))

If you don't care about the order of the elements in your result then there's a simpler way:

如果您不关心结果中元素的顺序,那么有一种更简单的方法:

c = a + b

回答by Nick T

If you care about order:

如果您关心订单:

#import operator
import itertools
a = [1,2]
b = ['a','b']
#c = list(reduce(operator.add,zip(a,b))) # slow.
c = list(itertools.chain.from_iterable(zip(a,b))) # better.

print cgives [1, 'a', 2, 'b']

print c[1, 'a', 2, 'b']

回答by John Machin

Parsing

解析

[j for i in zip(a,b) for j in i]

in your head is easy enough if you recall that the forand ifclauses are done in order, followed a final append of the result:

如果你记得forandif子句是按顺序完成的,然后是结果的最后附加,那么在你的头脑中就很容易了:

temp = []
for i in zip(a, b):
    for j in i:
        temp.append(j)

and would be easier had it have been written with more meaningful variable names:

如果用更有意义的变量名编写它会更容易:

[item for pair in zip(a, b) for item in pair]

回答by SeanM

An alternate method using index slicing which turns out to be faster and scales better than zip:

一种使用索引切片的替代方法,结果证明它比 zip 更快且扩展性更好:

def slicezip(a, b):
    result = [0]*(len(a)+len(b))
    result[::2] = a
    result[1::2] = b
    return result

You'll notice that this only works if len(a) == len(b)but putting conditions to emulate zip will not scale with a or b.

您会注意到,这仅适用len(a) == len(b)于模拟 zip 的条件不会随 a 或 b 缩放的情况。

For comparison:

比较:

a = range(100)
b = range(100)

%timeit [j for i in zip(a,b) for j in i]
100000 loops, best of 3: 15.4 μs per loop

%timeit list(chain(*zip(a,b)))
100000 loops, best of 3: 11.9 μs per loop

%timeit slicezip(a,b)
100000 loops, best of 3: 2.76 μs per loop

回答by Paul Fabing

def main():

drinks = ["Johnnie Walker", "Jose Cuervo", "Jim Beam", "Hyman Daniels,"]
booze = [1, 2, 3, 4, 5]
num_drinks = []
x = 0
for i in booze:

    if x < len(drinks):

        num_drinks.append(drinks[x])
        num_drinks.append(booze[x])

        x += 1

    else:

        print(num_drinks)

return

main()

主要的()

回答by Arvind S.P

c = []
c.extend(a)
c.extend(b)

回答by Samer Aamar

Here is a standard / self-explaining solution, i hope someone will find it useful:

这是一个标准/不言自明的解决方案,我希望有人会发现它有用:

a = ['a', 'b', 'c']
b = ['1', '2', '3']

c = []
for x, y in zip(a, b):
    c.append(x)
    c.append(y)

print (c)

output:

输出:

['a', '1', 'b', '2', 'c', '3']

Of course, you can change it and do manipulations on the values if needed

当然,如果需要,您可以更改它并对值进行操作