Home>Article>Backend Development> python如何实现列表交叉合并?
a=[1,2,3,4,5,6]
b=['a','b','c','d','e']
实现合并后[1,'a',2,'b',3,'c'......]这种类型
from compiler.ast import flatten a = [1, 2, 3, 4, 5, 6] b = ['a', 'b', 'c', 'd', 'e'] c = flatten(zip(a, b)) print c [1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e']
import itertools list(itertools.chain.from_iterable(zip(a,b)))
办法有多种,其中一种,解题思路是模拟队列,由黄哥所写。
a = [1, 2, 3, 4, 5, 6] b = ['a', 'b', 'c', 'd', 'e'] c = [] map(lambda x, y: c.extend([x,y]) if None not in [x,y] else None, a, b) print c [1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e']
有规律的交叉合并可以通过for循环来实现的吧