import numpy as np
dict_map = {5: 1, 20: 2, 25: 3}
labels1 = [5, 5, 20, 5, 20, 25, 25, 20, 5, 5, 5]
# `__getitem__`返回所给键对应的值。
labels2 = np.vectorize(dict_map.__getitem__)(labels1)
print(labels1)
print(labels2)
[5, 5, 20, 5, 20, 25, 25, 20, 5, 5, 5]
[1 1 2 1 2 3 3 2 1 1 1]
np.vectorize:
https://numpy.org/doc/stable/reference/generated/numpy.vectorize.html