1

Let's consider an array [1,2,3] say, what I would like to generate is the list containing the pairs [[1,2], [1,3], [2,3]]. This can be done using itertools. However, I would like to produce them using pure numpy operations, and no loops or branching is allowed.

A close solution is provided here, but it generates all the possible pairs, instead of a particular fashion as in my case.

Can you please suggest a way to do that? The array will always be 1D.

Also, this is my first question on SE. If it requires any edit, please let me know.

Roy_123
  • 31
  • 5
  • A slightly more general (pairs, triplets, etc.) solution is [here](https://stackoverflow.com/a/42202157/7207392) it has a loop, but since in your case the size of subsets is fixed at 2 this can be eliminated. Or you could use `triu_indices`. – Paul Panzer Mar 25 '18 at 14:46

1 Answers1

0

Here is a one-liner using np.triu_indices:

>>> a = np.array([1, 2, 3])
>>> a[np.transpose(np.triu_indices(len(a), 1))]
array([[1, 2],
       [1, 3],
       [2, 3]])
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99