Python 两个列表列表随机播放

标签 python list

我有两个列表:

a = [1, 2, 3, 4]
b = [5, 6, 7, 8]

我需要这样的东西:

c = [1, 5, 2, 6, 3, 7, 4, 8]

我使用这个解决方案:

c = list(reduce(lambda x, y: x + y, zip(a, b)))

有更好的方法吗?

最佳答案

使用列表理解:

>>> [x for tup in zip(a, b) for x in tup]
[1, 5, 2, 6, 3, 7, 4, 8]

上面的嵌套列表推导等同于下面的嵌套 for 循环(以防你感到困惑):

result = []
for tup in zip(a, b):
    for x in tup:
        result.append(x)

关于Python 两个列表列表随机播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17993008/

相关文章:

python - 使用 lxml 和 Python 解析嵌套的 xml

python - 优达学城 CS101 : Not sure if this nested dictionary question is wrong or I'm missing something

asp.net-mvc - ASP.NET MVC获取下拉列表值

Java - 列表、类型和继承(也许是通用的?)

python - Flask:如何通过点击链接来更改html中flask变量的值

python - 按键对字典进行排序,其中键是字符串和数字的混合,然后使用排序的 key_values 创建一个新字典

python - 使用颜色模型可视化光流

c# - 获取列表/数组的最后一个元素

android - 在 Android ListView 中消失的 ProgressBar

python - 假设 n > k 如何从 n 个数字的列表中找到 k 个最大的数字