I have an n-dimensional bool numpy array. I want to invert the bool value of a random item in the array. Inverting is easy, but I'm not sure how to best index a random element. I can generate a list of random positions along the n dimensions
indices = [np.random.randint(n) for n in array.shape]
However, how do I use that to index the corresponding element? array[indices] fetches elements at indices along the first dimension. array[*indices] does not work. I could do something like
a = array[indices[0]]
for index in indices[1:]:
a = a[index]
but I'd like to avoid loops. Is there a more elegant solution?
[(*indices,)]will work fine