Python 如何从所有排列中生成所有可能的组合?
我有一个Python中Python 如何从所有排列中生成所有可能的组合?,python,permutation,Python,Permutation,我有一个Python中K元素的所有排列列表,如下所示: import itertools perms = list(itertools.permutations(range(K), K)) 我想生成一个矩阵(或列表)M,包含这些排列的所有可能组合perms。此矩阵(或列表)的每个元素的大小为N。我该怎么做 例如,对于K=2,我会得到perms=[(0,1)、(1,0)]。对于N=3,我希望: M = [ [(0, 1), (0, 1), (0, 1)], [(0, 1), (0,
K
元素的所有排列列表,如下所示:
import itertools
perms = list(itertools.permutations(range(K), K))
我想生成一个矩阵(或列表)M
,包含这些排列的所有可能组合perms
。此矩阵(或列表)的每个元素的大小为N
。我该怎么做
例如,对于K=2
,我会得到perms=[(0,1)、(1,0)]
。对于N=3
,我希望:
M = [ [(0, 1), (0, 1), (0, 1)],
[(0, 1), (0, 1), (1, 0)],
[(0, 1), (1, 0), (0, 1)],
[(0, 1), (1, 0), (1, 0)],
[(1, 0), (0, 1), (0, 1)],
[(1, 0), (0, 1), (1, 0)],
[(1, 0), (1, 0), (0, 1)],
[(1, 0), (1, 0), (1, 0)] ]
M = [ [(0, 1), (0, 1)],
[(0, 1), (1, 0)],
[(1, 0), (0, 1)],
[(1, 0), (1, 0)] ]
M = [ [(0, 1), (1, 0)] ] = perms
M
是一个包含8个
列表的列表。每个列表的大小为N=3
,包含perms
中的元素
对于N=2
,我希望:
M = [ [(0, 1), (0, 1), (0, 1)],
[(0, 1), (0, 1), (1, 0)],
[(0, 1), (1, 0), (0, 1)],
[(0, 1), (1, 0), (1, 0)],
[(1, 0), (0, 1), (0, 1)],
[(1, 0), (0, 1), (1, 0)],
[(1, 0), (1, 0), (0, 1)],
[(1, 0), (1, 0), (1, 0)] ]
M = [ [(0, 1), (0, 1)],
[(0, 1), (1, 0)],
[(1, 0), (0, 1)],
[(1, 0), (1, 0)] ]
M = [ [(0, 1), (1, 0)] ] = perms
对于N=1
,我希望:
M = [ [(0, 1), (0, 1), (0, 1)],
[(0, 1), (0, 1), (1, 0)],
[(0, 1), (1, 0), (0, 1)],
[(0, 1), (1, 0), (1, 0)],
[(1, 0), (0, 1), (0, 1)],
[(1, 0), (0, 1), (1, 0)],
[(1, 0), (1, 0), (0, 1)],
[(1, 0), (1, 0), (1, 0)] ]
M = [ [(0, 1), (0, 1)],
[(0, 1), (1, 0)],
[(1, 0), (0, 1)],
[(1, 0), (1, 0)] ]
M = [ [(0, 1), (1, 0)] ] = perms
我不知道我是否正确地表述了我的问题(我认为它可以重新表述得比这更清楚)。考虑一下,可能有一种非常简单的方法可以使用
itertools获得您想要的结果。组合和集合:
import itertools
K = 2
N = 3
perms = list(itertools.permutations(range(K), K))
# The order matters so we need to copy the list N times
perms = perms*N
# Create the combinations
combs = itertools.combinations(perms, N)
# Only keep unique combinations
M = set(combs)
如果要将其作为列表列表,请使用:
M = [list(i) for i in M]
然后返回
[[(1, 0), (0, 1), (0, 1)],
[(1, 0), (1, 0), (1, 0)],
[(0, 1), (0, 1), (1, 0)],
[(0, 1), (1, 0), (1, 0)],
[(0, 1), (0, 1), (0, 1)],
[(0, 1), (1, 0), (0, 1)],
[(1, 0), (0, 1), (1, 0)],
[(1, 0), (1, 0), (0, 1)]]
您可以使用itertools
中的product
from itertools import permutations, product
perms = permutations(range(2))
cartesian_tuples = product(perms, repeat=3)
# (((0, 1), (0, 1), (0, 1)),
# ((0, 1), (0, 1), (1, 0)),
# ((0, 1), (1, 0), (0, 1)),
# ((0, 1), (1, 0), (1, 0)),
# ((1, 0), (0, 1), (0, 1)),
# ((1, 0), (0, 1), (1, 0)),
# ((1, 0), (1, 0), (0, 1)),
# ((1, 0), (1, 0), (1, 0)))
如果需要多次迭代,可以手动将单个零件转换为列表。当前结构由发电机组成,这些发电机在一次迭代后将耗尽,无法再次使用。如果需要嵌套列表:
cartesian_tuples = map(list, list(product(perms, repeat=3)))
# [[(0, 1), (0, 1), (0, 1)],
# [(0, 1), (0, 1), (1, 0)],
# [(0, 1), (1, 0), (0, 1)],
# [(0, 1), (1, 0), (1, 0)],
# [(1, 0), (0, 1), (0, 1)],
# [(1, 0), (0, 1), (1, 0)],
# [(1, 0), (1, 0), (0, 1)],
# [(1, 0), (1, 0), (1, 0)]]
在Python3.X中,您必须将其包装在另一个列表调用中,因为map(…)
返回一个map
对象
cartesian_tuples = list(map(list, list(product(perms, repeat=3))))
或者,您可以避免所有这些胡说八道,使用列表理解
cartesian_tuples = [[perm for perm in prod] for prod in product(perms, repeat=3)]
但每次需要迭代器时,最好创建一个新的迭代器
def product_of_permutations(n, k):
return product(permutations(range(k)), repeat=n)
为什么不[(1,0)、(1,0)、(0,1)]
?当您指定N
时,当K
小于N
时,您如何决定如何填充列表?这是我犯的错误。我将不得不接受一个答案,并重新问我认为的问题。只需更新您的问题。谢谢您的帮助。我很感激。我在这个问题上犯了一个错误。请查看编辑。感谢您的帮助。我很感激。我在这个问题上犯了一个错误。请看编辑。