I have a given numpy array, data. I want to change its values in the ascending order from 0. I could solve it by manually making the look up table, LUT.
import numpy as np
data = np.array([[2, 6, 16, 39, 43],
[43, 6, 16, 39, 2]])
LUT={2:0,
6:1,
16:2,
39:3,
43:4}
def changer(X):
res = np.copy(X)
for i, j in LUT.items():
res[X==i] = j
return res
result = changer(data)
print (result)
[[0 1 2 3 4]
[4 1 2 3 0]]
The result is correct as I expected. However, sometimes, I am lazy to make LUT manually. So, how could I get the same results programmatically?
edit
I tried to make dictionary, LUT as follows.
list = [2,6,16,39,43]
LUT = {}
for i in enumerate(list):
LUT.update({list[i]: i})
But,
D.update({list[i]: i})
TypeError: list indices must be integers, not tuple