简体   繁体   English

如何在二维数组中创建来自 2 个不同数组的所有组合

[英]How to create in a 2-dimentionals array all combinations from 2 different array

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] [1,2][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.例如,当我有 n(它是颜色数)= 5 并且 N = 6 --> RandomedColor= [Red,white,black,green,Yellow] and liste2 = [1,2,3,4,5, 6]至少需要5分钟才能完成。

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 !感谢您的帮助!

This can be implemented relatively efficiently with itertools.product() .这可以使用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.然后你可以用它来生成你需要的副本。

You can use product and combinations_with_replacement from itertools您可以使用itertools中的productcombinations_with_replacement

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: 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']]]

To handle combinatory problems, itertools is very usefull.要处理组合问题, itertools非常有用。 It offers all the required function to generate the combinations.它提供了生成组合所需的所有 function。

The idea is to use product and then combinations_with_replacement .这个想法是先使用product然后再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: 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'))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM