I have a numpy 2d integer array:
a = numpy.array([[1, 1, 2, 2],
[0, 1, 2, 2],
[1, 3, 2, 3]])
I have a lookup table (list of tuples) with original values and new values:
lookup = [(0, 1),
(1, 0),
(2, 255)]
My task is to reclassify the original array based on the lookup table: all zeros in original array should become ones, all ones should become zeros, all values==2 should change to 255, other values should stay unchanged. The expected result is:
[[0, 0, 255, 255],
[1, 0, 255, 255],
[0, 3, 255, 3]]
I tried the following solution:
for row in lookup:
original_value = row[0]
new_value = row[1]
a[a == original_value] = new_value
However, I did not get the desired result, the result of above operation is:
[[0, 0, 255, 255],
[0, 0, 255, 255],
[0, 3, 255, 3]]
notice result[1, 0] is 0 but it should be 1.
Is there method (other than a nested loop) to change values in the original array using my lookup table?