Python:两个列表交织在一起 [英] Python: Intertwining two lists

查看:51
本文介绍了Python:两个列表交织在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行以下操作的pythonic方法是什么:

What is the pythonic way of doing the following:

我有两个长度为 n 的列表 a b ,我想组成列表

I have two lists a and b of the same length n, and I want to form the list

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

推荐答案

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

阅读有关 zip 的文档.

要与Ignacio的答案进行比较,请参阅以下问题:

For comparison with Ignacio's answer see this question: How do I convert a tuple of tuples to a one-dimensional list using list comprehension?

这篇关于Python:两个列表交织在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆