i need to iterate over a tuple of indices. all indices must be in the range
[0, N) with the condition i > j. The toy example I present here deal
Here's an approach with itertools.combinations to have a generic number of levels -
map(tuple,(N-1-np.array(list(combinations(range(N),M))))[::-1])
Or a bit twisted one with same method -
map(tuple,np.array(list(combinations(range(N-1,-1,-1),M)))[::-1])
, where N : number of elements and M : number of levels.
Sample run -
In [446]: N = 5
...: for i in range(N):
...: for j in range(i):
...: for k in range(j): # Three levels here
...: print(i, j, k)
...:
(2, 1, 0)
(3, 1, 0)
(3, 2, 0)
(3, 2, 1)
(4, 1, 0)
(4, 2, 0)
(4, 2, 1)
(4, 3, 0)
(4, 3, 1)
(4, 3, 2)
In [447]: N = 5; M = 3
In [448]: map(tuple,(N-1-np.array(list(combinations(range(N),M))))[::-1])
Out[448]:
[(2, 1, 0),
(3, 1, 0),
(3, 2, 0),
(3, 2, 1),
(4, 1, 0),
(4, 2, 0),
(4, 2, 1),
(4, 3, 0),
(4, 3, 1),
(4, 3, 2)]