gpt4 book ai didi

python - 一次附加多个列表

转载 作者:行者123 更新时间:2023-12-01 01:39:40 25 4
gpt4 key购买 nike

将多个列表附加在一起的最Pythonic方法和最快方法(可能是相同的)是什么?例如,给出以下列表:

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

我们得到一个列表:

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

最佳答案

在Python 3.5+中,您可以使用通用解包:

combined = [*a, *b, *c, *d]

或者在Python 3.5+之前,您可以使用itertools.chain:

from itertools import chain
combined = list(chain(a, b, c, d))

关于python - 一次附加多个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52022590/

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