对于大型数组,以下仅 numpy 的代码可能会执行得更好:
arr = np.array([['foo', 'foot', 'oot'],
['foo', 'foot', 'oot'],
['bar', 'bart', 'art'],
['bar', 'bart', 'art']])
dict_ = {'foo' : 1, 'bar' : 2, 'foot' : 34, 'bart' : 54, 'oot' : 123}
arr_flat = arr.ravel()
keys = np.array(dict_.keys())
vals = np.array(dict_.values())
sort_idx = np.argsort(keys)
keys = keys[sort_idx]
vals = vals[sort_idx]
vals = np.concatenate((vals, [np.nan]))
unique, indices = np.unique(arr_flat, return_inverse=True)
locs = np.searchsorted(keys, unique, side='left')
no_match = unique != keys[locs]
locs[no_match] = len(keys)
new_arr = np.take(vals, np.take(locs, indices)).reshape(arr.shape)
# Same as new_arr = vals[locs[indices]].reshape(arr.shape)
>>> arr
array([['foo', 'foot', 'oot'],
['foo', 'foot', 'oot'],
['bar', 'bart', 'art'],
['bar', 'bart', 'art']],
dtype='|S4')
>>> new_arr
array([[ 1., 34., 123.],
[ 1., 34., 123.],
[ 2., 54., nan],
[ 2., 54., nan]])