从 2 个集合或列表中创建所有可能的组合作为字典

Create of all possible combinations from 2 sets or lists as a dict

我有 2 套(也可以是一个列表,但不确定它是否会有很大不同)

set1 = (w1,w2,w3,w4)
set2 = (r1,r2,r3,r4,r5,r6,r7)

我想将所有可能的组合创建为 集合字典 ,其中 set1 的元素作为键,set2 的元素作为值。一组值的最大长度为 n.

例如,n=3:

示例输出

Comb1 = {w1:(r1,r2), w2:(r3), w3:(r4,r5,r6), w4:(r7)}
Comb2 = {w1:(r6), w2:(r1,r3), w3:(r2), w4:(r4,r5,r7)}
Comb3 = {w1:(r2), w2:(r1,r3), w3:(r4,r6), w4:(r6,r7)}
.
.
.
.

此处,在每个组合中,一个集合(或值)的最大长度为 3。

我正在查看 itertools 库,但不确定如何对设置的可变长度进行采样。我也考虑过从 set2 中不进行替换的随机抽样,但我不会从中获得所有可能的组合。

您可以使用递归生成器函数:

import copy
set1, n = ('w1','w2','w3','w4'), 3
set2 = ('r1','r2','r3','r4','r5','r6','r7')
def combos(d, c = []):
   if not d:
      yield c
   else:
      for i, a in enumerate(d):
         if (c1:=len(c)) + 1 <= (l1:=len(set1)):
            yield from combos(d[:i]+d[i+1:], c+[[a]])
         if ((_d:=d[:i]+d[i+1:]) or c1 == l1) and c and len(c[-1]) + 1 <= n:
            yield from combos(_d, [*c[:-1], c[-1]+[a]])
         
r = combos(set2)
print([dict(zip(set1, map(tuple, next(r)))) for _ in range(100)])

输出(前 100 个结果):

[{'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4'), 'w4': ('r5', 'r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4'), 'w4': ('r5', 'r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4', 'r5'), 'w4': ('r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4', 'r5'), 'w4': ('r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4'), 'w4': ('r6', 'r5', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4'), 'w4': ('r6', 'r7', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4', 'r6'), 'w4': ('r5', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4', 'r6'), 'w4': ('r7', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4'), 'w4': ('r7', 'r5', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4'), 'w4': ('r7', 'r6', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4', 'r7'), 'w4': ('r5', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r4', 'r7'), 'w4': ('r6', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5'), 'w4': ('r4', 'r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5'), 'w4': ('r4', 'r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5', 'r4'), 'w4': ('r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5', 'r4'), 'w4': ('r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5'), 'w4': ('r6', 'r4', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5'), 'w4': ('r6', 'r7', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5', 'r6'), 'w4': ('r4', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5', 'r6'), 'w4': ('r7', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5'), 'w4': ('r7', 'r4', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5'), 'w4': ('r7', 'r6', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5', 'r7'), 'w4': ('r4', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r5', 'r7'), 'w4': ('r6', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6'), 'w4': ('r4', 'r5', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6'), 'w4': ('r4', 'r7', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6', 'r4'), 'w4': ('r5', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6', 'r4'), 'w4': ('r7', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6'), 'w4': ('r5', 'r4', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6'), 'w4': ('r5', 'r7', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6', 'r5'), 'w4': ('r4', 'r7')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6', 'r5'), 'w4': ('r7', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6'), 'w4': ('r7', 'r4', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6'), 'w4': ('r7', 'r5', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6', 'r7'), 'w4': ('r4', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r6', 'r7'), 'w4': ('r5', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7'), 'w4': ('r4', 'r5', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7'), 'w4': ('r4', 'r6', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7', 'r4'), 'w4': ('r5', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7', 'r4'), 'w4': ('r6', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7'), 'w4': ('r5', 'r4', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7'), 'w4': ('r5', 'r6', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7', 'r5'), 'w4': ('r4', 'r6')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7', 'r5'), 'w4': ('r6', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7'), 'w4': ('r6', 'r4', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7'), 'w4': ('r6', 'r5', 'r4')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7', 'r6'), 'w4': ('r4', 'r5')}, {'w1': ('r1',), 'w2': ('r2',), 'w3': ('r3', 'r7', 'r6'), 'w4': ('r5', 'r4')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4',), 'w4': ('r5', 'r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4',), 'w4': ('r5', 'r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r5'), 'w4': ('r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r5', 'r6'), 'w4': ('r7',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r5'), 'w4': ('r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r5', 'r7'), 'w4': ('r6',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4',), 'w4': ('r6', 'r5', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4',), 'w4': ('r6', 'r7', 'r5')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r6'), 'w4': ('r5', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r6', 'r5'), 'w4': ('r7',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r6'), 'w4': ('r7', 'r5')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r6', 'r7'), 'w4': ('r5',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4',), 'w4': ('r7', 'r5', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4',), 'w4': ('r7', 'r6', 'r5')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r7'), 'w4': ('r5', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r7', 'r5'), 'w4': ('r6',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r7'), 'w4': ('r6', 'r5')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r4', 'r7', 'r6'), 'w4': ('r5',)}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r5',), 'w4': ('r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r5', 'r6'), 'w4': ('r7',)}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r5',), 'w4': ('r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r5', 'r7'), 'w4': ('r6',)}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r6',), 'w4': ('r5', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r6', 'r5'), 'w4': ('r7',)}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r6',), 'w4': ('r7', 'r5')}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r6', 'r7'), 'w4': ('r5',)}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r7',), 'w4': ('r5', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r7', 'r5'), 'w4': ('r6',)}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r7',), 'w4': ('r6', 'r5')}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r4'), 'w3': ('r7', 'r6'), 'w4': ('r5',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5',), 'w4': ('r4', 'r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5',), 'w4': ('r4', 'r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r4'), 'w4': ('r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r4', 'r6'), 'w4': ('r7',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r4'), 'w4': ('r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r4', 'r7'), 'w4': ('r6',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5',), 'w4': ('r6', 'r4', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5',), 'w4': ('r6', 'r7', 'r4')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r6'), 'w4': ('r4', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r6', 'r4'), 'w4': ('r7',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r6'), 'w4': ('r7', 'r4')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r6', 'r7'), 'w4': ('r4',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5',), 'w4': ('r7', 'r4', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5',), 'w4': ('r7', 'r6', 'r4')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r7'), 'w4': ('r4', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r7', 'r4'), 'w4': ('r6',)}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r7'), 'w4': ('r6', 'r4')}, {'w1': ('r1',), 'w2': ('r2', 'r3'), 'w3': ('r5', 'r7', 'r6'), 'w4': ('r4',)}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r5'), 'w3': ('r4',), 'w4': ('r6', 'r7')}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r5'), 'w3': ('r4', 'r6'), 'w4': ('r7',)}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r5'), 'w3': ('r4',), 'w4': ('r7', 'r6')}, {'w1': ('r1',), 'w2': ('r2', 'r3', 'r5'), 'w3': ('r4', 'r7'), 'w4': ('r6',)}]