-1

I have the following 2d array [[2,3,4],[4,4,2] and the following dictionary {2:7,3:5,4:6}
I would like somehow to transform the array using the dictionary, i.e. have as an output the following result:

[[7,5,6],[6,6,7]]

Is there a simple (maybe built-in function) to do so?

DsCpp
  • 2,259
  • 3
  • 18
  • 46

1 Answers1

1

You can use np.vectorize():

x = np.array([[2,3,4],[4,4,2]])
y = {2:7,3:5,4:6}
np.vectorize(y.get)(x)

array([[7, 5, 6],
       [6, 6, 7]])
zipa
  • 27,316
  • 6
  • 40
  • 58