1

Given an np.arange(k) for any integer k value, I'm trying to find all its (k - 1)-combinations.

Example with k = 4 (ideally by this order):

array([[1, 2, 3],
       [0, 2, 3],
       [0, 1, 3],
       [0, 1, 2]])

Using regular python, this works:

[[j for j in range(k) if i != j] for i in range(k)]

But I was aiming for a more numpythonic approach. This is my best shot so far:

np.rot90(np.arange(k).repeat(k - 1).reshape((k - 1, k)))

Is there a cleaner way of doing it using numpy, or should I just use normal python?

cabralpinto
  • 1,814
  • 3
  • 13
  • 32

1 Answers1

0

hpaulj already pointed it out in his comment, but here is one way of approaching this using itertools combinations:

from itertools import combinations
import numpy as np


k = 4
a = np.array([*combinations(np.arange(k), 3)])
print(a)

Maybe you also want to have a look at this post which uses the sympy library to achieve similar stuff.

mapf
  • 1,906
  • 1
  • 14
  • 40