Home>Article>Backend Development> python如何实现列表交叉合并?

python如何实现列表交叉合并?

WBOY
WBOY Original
2016-06-06 16:24:35 3755browse

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))) 
办法有多种,其中一种,解题思路是模拟队列,由黄哥所写。 python如何实现列表交叉合并?
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循环来实现的吧
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn