gpt4 book ai didi

python - 在python中生成数组列的所有可能组合

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:09 24 4
gpt4 key购买 nike

比如我有这样的数组

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

我想从上面的列表中生成所有组合,如果它应该看起来像这样的话。

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

最佳答案

听起来像你想要的product来自内置 itertools图书馆

>>> import itertools
>>> list(itertools.product([1, 2, 3], [4], [5, 6]))
[(1, 4, 5), (1, 4, 6), (2, 4, 5), (2, 4, 6), (3, 4, 5), (3, 4, 6)]
>>>
>>> columns = [[1,2,3],
[4],
[5,6]]
>>> list(itertools.product(*columns))
[(1, 4, 5), (1, 4, 6), (2, 4, 5), (2, 4, 6), (3, 4, 5), (3, 4, 6)]

关于python - 在python中生成数组列的所有可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43492209/

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