challenging special numpy operation
I have a NumPy array that is full of indices of numbers input. I want to check if certain indices indices are in it. Say that the i'th row of input, input[i] has entries j_1&lt;...&lt;j_n that their values belong to indices. I would like to switch the value of input[i,j_n] with a random value from indices. How can I do it elegantly and efficiently?

Example:

input = [[1,2,3],[4,5,6],[7,8,9]]

indices = [2,4,6]

input --> [[1,4,3],[4,5,4],[7,8,9]]

Something like that:

import random

input = [[i if i not in indices else random.choice(indices) for i in x] for x in input]


Better, of course, to check in set instead of list:

import random

d = {*indices}
input = [[i if i not in d else random.choice(indices) for i in x] for x in input]

