I chose to indirectly index the return from numpy.nonzero
by using pop() on ndx_ndx list to get one (indirect) index into the input array without replacement
eventually ndx_ndx will be emptied when you have gotten all of the ones
import numpy as np
ary = np.random.randint(2, size=(20, 20))
# get the indices of all of the ones
ndx_ary_ones = np.nonzero(ary)
# make a range list for pointing into ndx_ary_ones
ndx_ndx = list(range(len(ndx_ary_ones[0])))
# randomize the order
np.random.shuffle(ndx_ndx)
# pop the last ndx_ndx
a_ran_ndx_ndx = ndx_ndx.pop()
# get the index tuple for the one in ary that we removed from ndx_ndx
a_ran_one_ndx = (ndx_ary_ones[0][a_ran_ndx_ndx],
ndx_ary_ones[1][a_ran_ndx_ndx])
# testing...
print('ary', ary, '\n')
print('ndx_ary_ones ', *ndx_ary_ones, sep = '\n')
print('\n','ndx_ndx[0:10] ', ndx_ndx[0:10], '\n')
for _ in range (10):
a_ran_ndx_ndx = ndx_ndx.pop()
a_ran_one_ndx = (ndx_ary_ones[0][a_ran_ndx_ndx],
ndx_ary_ones[1][a_ran_ndx_ndx])
print(a_ran_one_ndx, ary[a_ran_one_ndx])
ary [[0 0 0 ..., 1 1 1]
[0 1 1 ..., 1 1 1]
[1 0 0 ..., 1 0 1]
...,
[1 1 0 ..., 1 0 1]
[1 1 0 ..., 1 1 1]
[1 0 0 ..., 0 0 1]]
ndx_ary_ones
[ 0 0 0 ..., 19 19 19]
[ 3 5 7 ..., 14 15 19]
ndx_ndx[0:10] [121, 43, 146, 69, 64, 3, 29, 186, 98, 30]
(7, 12) 1
(8, 18) 1
(0, 3) 1
(10, 2) 1
(18, 18) 1
(17, 7) 1
(15, 14) 1
(4, 11) 1
(10, 1) 1
(4, 4) 1