gpt4 book ai didi

python - 如何在将元素 1 乘 1 的同时连接两个列表

转载 作者:行者123 更新时间:2023-12-02 09:45:12 25 4
gpt4 key购买 nike

如何在 Python 中连接两个列表并按 1 的顺序获取元素?

示例:

listone = [1, 2, 3]
listtwo = [4, 5, 6]

预期结果:

>>> joinedlist
[1, 4, 2, 5, 3, 6]

最佳答案

zip 列表并用 itertooos.chain 压平:

from itertools import chain

list(chain.from_iterable(zip(listone, listtwo)))
[1, 4, 2, 5, 3, 6]

关于python - 如何在将元素 1 乘 1 的同时连接两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61228346/

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