我有此代码:
from collections import Counter
def groups(d, l, c = []):
if l == len(c):
yield c
else:
for i in d:
if i not in c:
_c = Counter([j for k in [*c, i] for j in k])
if all(j < 3 for j in _c.values()):
yield from groups(d, l, c+[i])
data = [(1,2),(2,3),(2,4),(2,5),(2,6),(3,1),(3,2),(3,4)]
result = list(groups(data, 3))
此代码正在生成三对,例如:[[(1, 2), (2, 3), (3, 1)], [(1, 2), (2, 3), (3, 4)], [(1, 2), (2, 4), (3, 1)],1[(1, 2), (2, 4), (3, 4)], [(1, 2), (2, 5), (3, 1)] ...
问题是,有这样的重复项:
[(1, 2), (2, 3), (3, 1)] and [(2, 3), (1, 2), (3, 1)]
有没有一种方法可以避免它们在生成过程中发生?