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?
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)]