How about this C = B[A] . What is the beauty of numpy:
In [1]: import numpy as np In [2]: A = np.array([[1, 1, 0, 2], ...: [1, 0, 0, 2], ...: [1, 1, 0, 2]]) In [3]: B = np.array([0, 5, 3]) In [4]: B[A] Out[4]: array([[5, 5, 0, 3], [5, 0, 0, 3], [5, 5, 0, 3]])
source share