可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has an answer here:
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'
回答1:
Will this do? Sometimes, plain Python is a good, direct way to handle such things. The below builds a list of translations (easily converted back to a numpy array) and the joined output.
import numpy as np abc_array = np.array(['B', 'D', 'A', 'F', 'H', 'I', 'Z', 'J']) transdict = {'A': 'Adelaide', 'B': 'Bombay', 'C': 'Cologne', 'D': 'Dresden', 'E': 'Erlangen', 'F': 'Formosa', 'G': 'Gdansk', 'H': 'Hague', 'I': 'Inchon', 'J': 'Jakarta', 'Z': 'Zambia' } phoenetic = [transdict[letter] for letter in abc_array] print ' '.join(phoenetic)
The output from this is:
Bombay Dresden Adelaide Formosa Hague Inchon Zambia Jakarta
回答2:
With brute-force NumPy broadcasting
-
idx = np.nonzero(transdict.keys() == abc_array[:,None])[1] out = np.asarray(transdict.values())[idx]
With np.searchsorted
based searching and indexing -
sort_idx = np.argsort(transdict.keys()) idx = np.searchsorted(transdict.keys(),abc_array,sorter = sort_idx) out = np.asarray(transdict.values())[sort_idx][idx]
Sample run -
In [1]: abc_array = np.array(['B', 'D', 'A', 'B', 'D', 'A', 'C']) ...: transdict = {'A': 'Adelaide', 'B': 'Bombay', 'C': 'Cologne', 'D': 'Delhi'} ...: In [2]: idx = np.nonzero(transdict.keys() == abc_array[:,None])[1] ...: out = np.asarray(transdict.values())[idx] ...: In [3]: out Out[3]: array(['Bombay', 'Delhi', 'Adelaide', 'Bombay', 'Delhi', 'Adelaide', 'Cologne'], dtype='|S8') In [4]: sort_idx = np.argsort(transdict.keys()) ...: idx = np.searchsorted(transdict.keys(),abc_array,sorter = sort_idx) ...: out = np.asarray(transdict.values())[sort_idx][idx] ...: In [5]: out Out[5]: array(['Bombay', 'Delhi', 'Adelaide', 'Bombay', 'Delhi', 'Adelaide', 'Cologne'], dtype='|S8')