简体   繁体   English

itertools排列和组合

[英]itertools permutations and combinations

I am struggling with using itertools permutations and combinations together. 我正在努力使用itertools排列和组合。 Ultimately, I am trying to create a matrix of possible combinations of Customers across various permutations of machines. 最终,我试图在各种机器排列中创建一个可能的客户组合矩阵。 I believe I have the combinations piece, but have not been able to add permutations to the script. 我相信我有组合片,但无法在脚本中添加排列。

Here's my code so far: 到目前为止,这是我的代码:

import itertools

Mach = [1,2,3,4]
Cust = [1,2,3,4,5,6,7,8,9,10,11,12]

a = len(Cust)

for n in range(a):
    print list(itertools.combinations(Cust,n))
    n = n+1

Ideally, I would like to solve all possible outputs of: 理想情况下,我想解决以下所有可能的输出:

1 - 1,2,3
2 - 4,5,6
3 - 7,8,9
4 - 10,11,12

Any help or direction would be appreciated. 任何帮助或方向将不胜感激。

Update: Forgive my ignorance, using Product is not necessarily providing the results that I was driving towards. 更新:原谅我的无知,使用产品并不一定能提供我正在推动的结果。 What am I trying to do is create a list of Customers on Machines, with each Customer only reflected on one machine (at a time), and then iteratively creating another matrix of this combination; 我要做的是创建一个机器上的客户列表,每个客户只反映在一台机器上(一次),然后迭代地创建这个组合的另一个矩阵; for all possible combinations. 对于所有可能的组合。 I believe this is a combination, not a permutation problem, as for the output, I do consider 1: 1, 2, 3 and 1: 3, 2, 1 to be the same. 我认为这是一个组合,而不是排列问题,对于输出,我认为1:1,2,3和1:3,2,1是相同的。

Example: (Cust1, Mach1); 示例:(Cust1,Mach1); (Cust2, Mach1); (Cust2,Mach1); (Cust3, Mach2); (Cust3,Mach2); (Cust4, Mach2); (Cust4,Mach2); (Cust5, Mach2); (Cust5,Mach2); (Cust6, Mach3); (Cust6,Mach3); (Cust7, Mach3); (Cust7,Mach3); (Cust8, Mach3); (Cust8,Mach3); (Cust9, Mach3); (Cust9,Mach3); (Cust10, Mach3); (Cust10,Mach3); (Cust11, Mach4); (Cust11,Mach4); (Cust12, Mach4) (Cust12,Mach4)

Followed by (as an example): (Cust1, Mach1); 接下来(作为例子):( Cust1,Mach1); (Cust2, Mach2); (Cust2,Mach2); (Cust3, Mach2); (Cust3,Mach2); (Cust4, Mach2); (Cust4,Mach2); (Cust5, Mach2); (Cust5,Mach2); (Cust6, Mach3); (Cust6,Mach3); (Cust7, Mach3); (Cust7,Mach3); (Cust8, Mach3); (Cust8,Mach3); (Cust9, Mach3); (Cust9,Mach3); (Cust10, Mach4); (Cust10,Mach4); (Cust11, Mach4); (Cust11,Mach4); (Cust12, Mach4) (Cust12,Mach4)

etc... 等等...

Neither product nor combinations is really what you want. productcombinations都不是你想要的。 You want to pair each item of Mach with a set of items from Cust . 您想要将每个Mach项与来自Cust项配对。

n = len(cust)/len(m)
for i, m in enumerate(mach):
    print(m, cust[n*i: n*(i+1)])

Here is a recursive solution which uses itertools.combination . 这是一个使用itertools.combination的递归解决方案。 The idea is to choose the combination for the first machine and then recursively generate combinations for the remaining customers and machines. 我们的想法是为第一台机器选择组合,然后递归地为剩余的客户和机器生成组合。

This solution was developped in Python3, but should work for Python2. 此解决方案是在Python3中开发的,但应该适用于Python2。

Code

import itertools

def group_combinations(machines, customers):
    if not machines:
        yield {}
    else:
        for group in itertools.combinations(customers, len(customers) // len(machines)):
            remaining = [c for c in customers if c not in group]
            for others in group_combinations(machines[1:], remaining):
                arrangement = {machines[0]: group}
                arrangement.update(others)
                yield arrangement

Example

machines = [1, 2]
customers = [1, 2, 3, 4]
groups = group_combinations(machines, customers)

for comb in groups:
    print(comb)

Output 产量

{1: (1, 2), 2: (3, 4)}
{1: (1, 3), 2: (2, 4)}
{1: (1, 4), 2: (2, 3)}
{1: (2, 3), 2: (1, 4)}
{1: (2, 4), 2: (1, 3)}
{1: (3, 4), 2: (1, 2)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM