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



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

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',)}]

相关内容

  • 没有找到相关文章

最新更新