I have a numpy array, which has hundreds of elements which are capital letters, in no particular order
import numpy as np
abc_array = np.array(['B', 'D', 'A', 'F', 'H', 'I', 'Z', 'J', ...])
Each element in this numpy.ndarray
is a numpy.string_
.
I also have a "translation dictionary", with key/value pairs such that the capital letter corresponds to a city
transdict = {'A': 'Adelaide', 'B': 'Bombay', 'C': 'Cologne',...}
There are only 26 pairs in the dictionary transdict
, but there are hundreds of letters in the numpy array I must translate.
What is the most efficient way to do this?
I have considered using numpy.core.defchararray.replace(a, old, new, count=None)[source]
but this returns a ValueError
, as the numpy array is a different size that the dictionary keys/values.
AttributeError: 'numpy.ndarray' object has no attribute 'translate'