gpt4 book ai didi

python - 交错4个相同长度的python列表

转载 作者:IT老高 更新时间:2023-10-28 20:29:41 25 4
gpt4 key购买 nike

我想在 python 中交错 4 个相同长度的列表。

我搜索了这个站点,只看到如何在 python 中交错 2: Interleaving two lists in Python

可以为 4 个列表提供建议吗?

我有这样的列表

l1 = ["a","b","c","d"]
l2 = [1,2,3,4]
l3 = ["w","x","y","z"]
l4 = [5,6,7,8]

我想要这样的列表

l5 = ["a",1,"w",5,"b",2,"x",6,"c",3,"y",7,"d",4,"z",8]

最佳答案

如果列表长度相同,zip()可用于交错四个列表,就像在您链接的问题中用于交错两个列表一样:

>>> l1 = ["a", "b", "c", "d"]
>>> l2 = [1, 2, 3, 4]
>>> l3 = ["w", "x", "y", "z"]
>>> l4 = [5, 6, 7, 8]
>>> l5 = [x for y in zip(l1, l2, l3, l4) for x in y]
>>> l5
['a', 1, 'w', 5, 'b', 2, 'x', 6, 'c', 3, 'y', 7, 'd', 4, 'z', 8]

关于python - 交错4个相同长度的python列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50808757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com