Вот один на основе np.isin
-
def create_uniques(arr):
# Get unique ones and the respective counts
unq,c = np.unique(arr,return_counts=1)
# Get mask of matches from the arr against the ones that have
# respective counts > 1, i.e. the ones with duplicates
m = np.isin(arr,unq[c>1])
# Get the ones that are absent in original array and shuffle it
newvals = np.setdiff1d(np.arange(len(arr)),arr[~m])
np.random.shuffle(newvals)
# Assign the shuffled values into the duplicate places to get final o/p
arr[m] = newvals
return ar
Образцы прогонов -
In [53]: arr = np.array([9, 7, 0, 4, 7, 4, 2, 2, 3, 7])
In [54]: create_uniques(arr)
Out[54]: array([9, 7, 0, 1, 6, 4, 8, 2, 3, 5])
In [55]: arr = np.array([9, 7, 0, 4, 7, 4, 2, 2, 3, 7])
In [56]: create_uniques(arr)
Out[56]: array([9, 4, 0, 5, 6, 2, 7, 1, 3, 8])
In [57]: arr = np.array([9, 7, 0, 4, 7, 4, 2, 2, 3, 7])
In [58]: create_uniques(arr)
Out[58]: array([9, 4, 0, 1, 7, 2, 6, 8, 3, 5])