0

I have two lists of 12 items and I want to compute the following:

-12 sets of combinations of the items so each item have been combined with each other on the other list after the 12 times. Simplified example:

list_a = ['a','b','c','d']
list_b = ['blue','green','red','white']

The 4 sets of combinations would be:

[
[['a','blue'], ['b','green'], ['c','red'], ['d','white']],
[['a','white'], ['b','red'], ['c','green'], ['d','blue']],
[['a','green'], ['b','white'], ['c','blue'], ['d','red']],
[['a','red'], ['b','blue'], ['c','white'], ['d','green']]
]

I tried with itertools but I get out of memory errors

Edit: I need to get the set with unique combinations. Not ['a','blue'],['a','green']...

More like in the example that I posted earlier

2
  • Please provide the itertools code? Since you need 144 combinations, it seems something wrong to have OOM error Commented Oct 7, 2022 at 1:28
  • import itertools; list(itertools.product(list_a, list_b)) will do what you want Commented Oct 7, 2022 at 1:31

2 Answers 2

0

As far as I've understood what you are trying to achieve, I came up with the following algorithm. You generate every rotation of b_list and then map each value at index i in a_list to the corresponding value at index i in b_list. Here is the implementation of the explained approach:

list_a = ['a', 'b', 'c', 'd']
list_b = ['blue', 'green', 'red', 'white']

combinations = []

for start in range(len(list_b)):
    combination = []

    for i in range(len(list_b)):
        combination.append([list_a[i], list_b[(start + i) % len(list_b)]])

    combinations.append(combination)
    
print(combinations)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your response. With this I get ['a', 'blue'], ['a', 'green'], ['a', 'red'] while I need every item to repeat only once in every set of combinations
I don't really understand what you mean. Does the order of the combinations matter?
Oh, I think I'm starting to get what you want to achieve. Do you only want the rotations of the combinations or do all the permutations matter?
I will edit my answer with an updated one.
Think as if 12 brothers had 12 shirts to share. They want to organise so in 12 days everyone would have worn every shirt once.
|
-1
def combine(list_a, list_b):
res = []
for i in range(len(list_a)):
    res.append([])
    for j in range(len(list_b)):
        res[-1].append([list_a[j], list_b[(j + i) % len(list_b)]])
return res

list_a = ['a', 'b', 'c', 'd']
list_b = ['blue', 'green', 'red', 'white']
print(combine(list_a, list_b))

I have this output:

[[['a', 'blue'], ['b', 'green'], ['c', 'red'], ['d', 'white']], 
[['a', 'green'], ['b', 'red'], ['c', 'white'], ['d', 'blue']], 
[['a', 'red'], ['b', 'white'], ['c', 'blue'], ['d', 'green']], 
[['a', 'white'], ['b', 'blue'], ['c', 'green'], ['d', 'red']]]

1 Comment

This is a duplicate answer to mine with no added insight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.