I want to create N unordered pairs of items (no repeat pairs, no pairs with repeated items), where each item is only used twice. For example, given the minimal amount of integers greater than 0, one possible set of 4 items would be:
[(0, 1), (0, 2), (3, 1), (3, 2)]
Another valid example would be:
[(0, 1), (1, 2), (2, 3), (3, 0)]
This would be invalid due to repeated pair:
[(0, 1), (0, 1), (2, 3), (3, 1)]
This would be invalid due to the item 0
being used three times:
[(0, 1), (0, 2), (0, 3), (3, 2)]
Also, to be clear the pair (0, 0)
is invalid because it is a pair with a repeated item.
My current code for accomplishing this is clumsy and only works multiples of 4. For now, let's assume the code only needs to work for multiples of 4. How could I do this with a better iterator?
num_set = range(4)
res = []
for n1, n2, n3, n4 in zip(num_set[::4], num_set[3::4], num_set[1::4], num_set[2::4]):
res.append((n1, n3))
res.append((n1, n4))
res.append((n2, n3))
res.append((n2, n4))
print(res)
I feel like I should be able to use itertools.combinations(range(4), 2)
and some filtering to accomplish this, but I'm not sure how.