python - itertools : Cartesian product of permutations

标签 python permutation python-itertools cartesian-product

<分区>

使用 pythons itertools,我想在一堆列表的所有排列的外积上创建一个迭代器。一个明确的例子:

import itertools
A = [1,2,3]
B = [4,5]
C = [6,7]

for x in itertools.product(itertools.permutations(A),itertools.permutations(B),itertools.permutations(C)):
    print x

虽然这可行,但我想将其概括为任意列表列表。我试过:

for x in itertools.product(map(itertools.permutations,[A,B,C])):
    print x

但它并没有达到我的预期。预期的输出是:

((1, 2, 3), (4, 5), (6, 7))
((1, 2, 3), (4, 5), (7, 6))
((1, 2, 3), (5, 4), (6, 7))
((1, 2, 3), (5, 4), (7, 6))
((1, 3, 2), (4, 5), (6, 7))
((1, 3, 2), (4, 5), (7, 6))
((1, 3, 2), (5, 4), (6, 7))
((1, 3, 2), (5, 4), (7, 6))
((2, 1, 3), (4, 5), (6, 7))
((2, 1, 3), (4, 5), (7, 6))
((2, 1, 3), (5, 4), (6, 7))
((2, 1, 3), (5, 4), (7, 6))
((2, 3, 1), (4, 5), (6, 7))
((2, 3, 1), (4, 5), (7, 6))
((2, 3, 1), (5, 4), (6, 7))
((2, 3, 1), (5, 4), (7, 6))
((3, 1, 2), (4, 5), (6, 7))
((3, 1, 2), (4, 5), (7, 6))
((3, 1, 2), (5, 4), (6, 7))
((3, 1, 2), (5, 4), (7, 6))
((3, 2, 1), (4, 5), (6, 7))
((3, 2, 1), (4, 5), (7, 6))
((3, 2, 1), (5, 4), (6, 7))
((3, 2, 1), (5, 4), (7, 6))

最佳答案

您错过了将列表解压缩为 3 个参数的 *

itertools.product(*map(itertools.permutations,[A,B,C]))

关于python - itertools : Cartesian product of permutations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12064780/

相关文章:

python - 以不同的顺序生成 itertools.product

python : cant open file 'hw.py' : [Errno 2] No such file or directory

python - 使 Python 3.x Slack (slackclient) 使用企业代理

python - 将 Matplotlib 图形嵌入 Tkinter 在 Python 3.5 上崩溃

java - 置换/加扰java中的arraylist元素

python - Itertools.按严格顺序排列列表 python

python - 列上的 Pandas GroupBy 和 CumSum

python - 这是从 df 获取一堆索引排列的最佳方法

string - Julia:如何获得给定字符串 s 的随机排列?

python - 两个列表(男孩和女孩)的所有可能(一夫一妻制)配对