Python:缠绕两个列表

2024-06-17 12:28:08 发布

您现在位置:Python中文网/ 问答频道 /正文

Python做以下事情的方式是什么:

我有两个长度相同的列表a和{},我想形成这个列表

c = [a[0], b[0], a[1], b[1], ..., a[n-1], b[n-1]]

Tags: 列表方式事情
3条回答
c = [item for pair in zip(a, b) for item in pair]

阅读有关zip的文档。在


如需与Ignacio的答案进行比较,请参阅以下问题:How do I convert a tuple of tuples to a one-dimensional list using list comprehension?

c = [item for t in zip(a,b) for item in t]
c = list(itertools.chain.from_iterable(itertools.izip(a, b)))

相关问题 更多 >