How to use a dictionary to translate/replace elements of an array? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:28:01

问题:

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') 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!