gpt4 book ai didi

python - 如何使用三个列表创建所有排列? Python

转载 作者:行者123 更新时间:2023-12-01 07:02:27 26 4
gpt4 key购买 nike

假设我有三个列表。

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

我想用 a、b 和 c 创建所有排列。

from itertools import permutations
a=[1,2,3,4,5,6,7,8,9,10]
b=[1,2,3,4,5,6,7,8,9,10]
c=[1,2,3,4,5,6,7,8,9,10]

perm = permutations([a,b,c])
for i in list(perm):
print (i)

这给出

([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

这不是我要找的。

我正在寻找解决方案

[1,1,1],[1,2,1],[1,3,1],[1,4,1].....

这可能吗

最佳答案

我认为您正在寻找产品:

from itertools import product
a=[1,2,3,4,5,6,7,8,9,10]

perm = product(a, repeat=3)
for i in list(perm):
print (i)

如果 a、b、c 不是同一个列表,请使用:product(a, b, c)

关于python - 如何使用三个列表创建所有排列? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578026/

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