I am trying to randomly pair two items from a list together in as many combinations as possible, then separate these into multiple lists where each item is included on each list and each pair is included exactly once in all the lists combined.
So for example if the following items were on the original list: banana apple peach pear
I would want to produce the following as separate lists:
banana apple
peach pear
banana peach
apple pear
banana pear
apple peach
I added a list of 12 fruits then found every combination of pairs from the list using itertools. I made several new empty lists and created a for loop to check if either item in the pair has already been used in each list, and if it has to assign it to a new list:
fruitcombos = list(it.combinations(fruits, 2))
s1 = []
s2 = []
...
s10 = []
for group in fruitcombos:
if not any(group[0] in x for x in s1) and not any(group[1] in x for x in s1):
s1.append(group)
elif not any(group[0] in x for x in s2) and not any(group[1] in x for x in s2):
s2.append(group)
elif not any(group[0] in x for x in s3) and not any(group[1] in x for x in s3):
s3.append(group)
...
elif not any(group[0] in x for x in s10) and not any(group[1] in x for x in s10):
s10.append(group)
I've looked at the itertools module and tried round_robin, ncycles, sliding_window, and others but cannot figure out how to solve this problem in a more elegant way. It also isn't working properly - with the loop I've tried, the first list (s1) has 12 of the 12 items as expected. However other lists decrease in number, with the fourth list onwards only including 8 of the 12 items because parts of the other pairs have already been included in other lists. How would I avoid this?
I would greatly appreciate any help.
Here is an example; I may have over-looked the reins.
Utilize the itertools.permutations function, applying each to a dictionary.
Output
Edit
Similarly, start by generating all combinations of pairs.
We'll limit the factor, by qualifying the iteration; in this case to, "banana".
From here, traverse each pair, generating a filtered list.
Output
Here is an output, for a though z.