1

I would like to be able to generate all unique permutations of a 2d array in python and keep the order. Let's say I have a 2D matrix [[1, 2, 3], [4, 5, 6]]. Expected result should be a in 8 x 3 in the form [[1, 2, 3], [1, 2, 6], [1, 5, 3], [1, 5, 6], [4, 2, 3], [4, 2, 6], [4, 5, 3], [4, 5, 6]].

Thanks

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
user129994
  • 23
  • 3

2 Answers2

0

Transpose the array, and then use itertools.product:

from itertools import product

list(map(list, product(*zip(*data))))

This outputs:

[[1, 2, 3], [1, 2, 6], [1, 5, 3], [1, 5, 6], [4, 2, 3], [4, 2, 6], [4, 5, 3], [4, 5, 6]]
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
0

The method with product would be the most readible way to do this, but since your question is tagged , here is how you can do this using only Numpy methods.

1. Permutations without expected order

You can use this method if order is not important for you. This uses np.meshgrid and np.stack with some .reshape to get the permutations you need, minus the order you expect.

import numpy as np

lst = [[1, 2, 3], [4, 5, 6]]

arr = np.array(lst)
np.stack(np.meshgrid(*arr.T),-1).reshape(-1,3)
array([[1, 2, 3],
       [1, 2, 6],
       [4, 2, 3],
       [4, 2, 6],
       [1, 5, 3],
       [1, 5, 6],
       [4, 5, 3],
       [4, 5, 6]])

2. Permutations with expected order

Getting the order to work is a bit "hacky" but a small modification over the above array with simple reordering of columns can solve this with pretty much the same code.

import numpy as np

lst = [[1, 2, 3], [4, 5, 6]]
order = [1,0,2]

arr = np.array(lst)[:,order]
np.stack(np.meshgrid(*arr.T),-1).reshape(-1,3)[:,order]
array([[1, 2, 3],
       [1, 2, 6],
       [1, 5, 3],
       [1, 5, 6],
       [4, 2, 3],
       [4, 2, 6],
       [4, 5, 3],
       [4, 5, 6]])
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51