Suppose I have a numpy array that consists of pairs of values. I'd like to find all combinations of the pairs without tearing them apart. Particularly, I was hoping for a numpy.meshgrid solution for this.
Imagine an array constructed like:
ab = np.array([[1,10], [2,20], [3,30], [4,40]])
Then my desired output is
>>> out: ([1,10], [2,20])
([1,10], [3,30])
([1,10], [4,40])
([2,20], [3,30])
([2,20], [4,40])
([3,30], [4,40])
The output can be either a np.array, or a tuple (I can convert accordingly afterwards). Please notice how duplicates are omitted in my results, neglecting the order of my couples (if [[1,10], [2,20]] is already there, I don't want [[2,20], [1,10]] in my output). For the real case, ab is of size 30,000, so speed is another issue.
That's why I tried meshgrid in the first place. For the simple case of single values, this is easily done (yet, still with the duplicates):
a = np.array([1,2,3,4])
mesh = np.array(np.meshgrid(a,a)).T.reshape(-1,2)
>>> out: [[1 1]
[1 2]
[1 3]
[1 4]
[2 1]
[...]
[4 4]]
but for my pairs, my attempt of
mesh = np.array(np.meshgrid(ab,ab)).T
gives me
[[[ 1 1]
[ 1 10]
[ 1 2]
[ 1 20]
[ 1 3]
[ 1 30]
[ 1 4]
[ 1 40]]
[[10 1]
[10 10]
[10 2]
[10 20]
...
[40 3]
[40 30]
[40 4]
[40 40]]]
In other words: meshgrid breaks up my pairs. I assume the solution is near, but I couldn't come up with it on my own. Any help is appreciated, thanks!