Home > Software design >  Create all possible combinations of lists of different sizes in numpy
Create all possible combinations of lists of different sizes in numpy

Time:07-30

I want to create a numpy array with all possible combinations of items from multiple lists of different sizes:

a = [1, 2] 
b = [3, 4]
c = [5, 6, 7] 
d = [8, 9, 10]

In each combination, I want 2 elements. I don't want any duplicates, and I don't want items from the same list to mix together.

I can get all such combinations with 3 elements with np.array(np.meshgrid(a, b, c, d)).T.reshape(-1,3) but I need pairs, not triplets. Doing np.array(np.meshgrid(a, b, c, d)).T.reshape(-1,2) doesn't work because it just cuts off one column of the original array.

Any ideas on how to achieve this?

CodePudding user response:

Similar to Olvin Roght's comment, but if you put your sublists in a list you can do:

>>>> ls = [[1,2],[3,4],[5,6,7],[8,9,10]]
>>>> [item for cmb in combinations(ls, 2) for item in product(*cmb)]
[(1, 3), (1, 4), (2, 3), (2, 4), (1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (1, 8), (1, 9), (1, 10), (2, 8), (2, 9), (2, 10), (3, 5), (3, 6), (3, 7), (4, 5), (4, 6), (4, 7), (3, 8), (3, 9), (3, 10), (4, 8), (4, 9), (4, 10), (5, 8), (5, 9), (5, 10), (6, 8), (6, 9), (6, 10), (7, 8), (7, 9), (7, 10)]

CodePudding user response:

So Itertools is great for this. The first thing you want to do is conjoin your list into a single iterable list (list of lists). The first step is to get all combinations of list.

from itertools import combinations, product

a = [1, 2] 
b = [3, 4]
c = [5, 6, 7] 
d = [8, 9, 10]
total = [a,b,c,d]
for item in combinations(total, 2):
    print(item)

which returns

([1, 2], [3, 4])
([1, 2], [5, 6, 7])
([1, 2], [8, 9, 10])
([3, 4], [5, 6, 7])
([3, 4], [8, 9, 10])
([5, 6, 7], [8, 9, 10])

The you can simply iterate over the individual lists as below

from itertools import combinations

a = [1, 2] 
b = [3, 4]
c = [5, 6, 7] 
d = [8, 9, 10]
total = [a,b,c,d]
for item in combinations(total, 2):
    for sub_item in item[0]:
        for second_sub_item in item[1]:
            print(sub_item, second_sub_item)

print out is

1 3
1 4
2 3
2 4
1 5
1 6
1 7
2 5
2 6
2 7
1 8
1 9
1 10
2 8
2 9
2 10
3 5
3 6
3 7
4 5
4 6
4 7
3 8
3 9
3 10
4 8
4 9
4 10
5 8
5 9
5 10
6 8
6 9
6 10
7 8
7 9
7 10
  • Related