1

I have to create a multiple dimensional list that looks like this :

[[[1,R],[1,R]],[[1,B],[1,B]],[[2,R],[1,B]],[[2,R],[2,B]]]...

From two lists :

[1,2] and [R,B]

My objective is to create all the possible combinations.

I've done something that is working. with a while, but when the number of combinations is huge it takes so much time. For exemple when i have n(it is the number of color) = 5 And N = 6 --> RandomedColor= [Red,white,black,green,Yellow] and liste2 = [1,2,3,4,5,6] it will take at least 5 mins to be done.

def Create_All_list_from_n(N): #Create all possible combinations of numbers with colors. if n = 2, and N = 5 then, there will be 2**5 combinations.
    all_list_of_N = []
    while(len(all_list_of_N) != n**N):     
        current_list = []
        for i in range(1,N+1):
            current_list.append([random.choice(RandomedColors),i])
        if current_list not in all_list_of_N:
            all_list_of_N.append(current_list)
        print(all_list_of_N)
        print(len(all_list_of_N))
    return all_list_of_N

Thanks for helping !

3 Answers 3

1

This can be implemented relatively efficiently with itertools.product().

For example:

import itertools


print(list(itertools.product([1, 2], ['R', 'B'])))
# [(1, 'R'), (1, 'B'), (2, 'R'), (2, 'B')]

and then you can use that to generate the replicas you need.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use product and combinations_with_replacement from itertools

from itertools import product, combinations_with_replacement
x = [1,2]
y = ['R','B']
products = [list(i) for i in product(x,y)]
print([list(i) for i in combinations_with_replacement(products,2)])

Output:

[[[1, 'R'], [1, 'R']], [[1, 'R'], [1, 'B']], [[1, 'R'], [2, 'R']], [[1, 'R'], [2, 'B']], [[1, 'B'], [1, 'B']], [[1, 'B'], [2, 'R']], [[1, 'B'], [2, 'B']], [[2, 'R'], [2, 'R']], [[2, 'R'], [2, 'B']], [[2, 'B'], [2, 'B']]]

1 Comment

As you can see in his example, he has the combination [1, 'R'], [1, 'R']. As per my answer, you should use combinations_with_replacement() instead of combinations().
0

To handle combinatory problems, itertools is very usefull. It offers all the required function to generate the combinations.

The idea is to use product and then combinations_with_replacement.

L1 = [1,2]
L2 = ["R" ,"B"]

import itertools

products = list(itertools.product(L1, L2))
combinations = list(itertools.combinations_with_replacement(products, 2))

Output:

[((1, 'R'), (1, 'R')),
 ((1, 'R'), (1, 'B')),
 ((1, 'R'), (2, 'R')),
 ((1, 'R'), (2, 'B')),
 ((1, 'B'), (1, 'B')),
 ((1, 'B'), (2, 'R')),
 ((1, 'B'), (2, 'B')),
 ((2, 'R'), (2, 'R')),
 ((2, 'R'), (2, 'B')),
 ((2, 'B'), (2, 'B'))]

Comments

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.