-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?

2
  • I don't think there is an inbuilt function to do exactly what you want. Commented Jun 12, 2019 at 10:05
  • Tip to get a good answer: Please do add your previously written code to the question. Also, you missed a closing bracket in [[2,3,4],[4,4,2]. Commented Jun 12, 2019 at 10:05

1 Answer 1

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]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.