I have a NumPy array that looks like this:
array(['_', '_', '_', '_', '_', '_', '_', '_', '_', '1', '1', '_', '_',
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '1', '1', '1',
'1', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_',
'_', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'e', '_',
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_',
'_', '_', '_'], dtype='<U1')
I'm looking for a way to replace the "_" characters with random choices from the ascii lowercase, digits, and punctuation characters:
abcdefghijklmnopqrstuvwxyz0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
Note that I'm trying to get a different random character for each element, while preserving the non "_" characters as they are. I've tried this:
rng = np.random.default_rng(42)
chars = string.ascii_lowercase+string.digits+string.punctuation
array[array=="_"] = chars[rng.choice(68,1)[0]]
but it gives the same random character each time. Thanks in advance for any suggestions.
rng.choice(68, 1)the second argument1is number of samples