I have a two-dimensional numpy array and my assignment was to get several arrays, each of which would be a set of sub-arrays in which the maximum element index is the same.
For example, [[1,2,3], [1,7,2], [1,7, 8]]
need to be filtered like this:
array with the maximum first element == []
array with the maximal second element == [[1,7,2]]
array with the maximum third element == [[1,2,3], [1,7, 8]]
And I thought it would be good to use dictionaries whose key would be the index of the maximum element, and whose value would be a lambda filter to get the mask of the original numpy array to apply it to the original array.
But then the strange things started to happen.
I have tried several options for creating a filtering dictionary. However. It seems that when iterating, the link to "i" is replaced, and since the link, not the value, is replaced, all lambda's start referring to the last "i" as well
otv_points = {str(i): lambda x: x == i for i in range(4)}
print(otv["0"](0)) #False Although it should be True
print(otv_points["0"](3)) #True Although it should be False
otv_points = {str(i): lambda x: x == int(i) for i in range(3)}
print(otv["0"](0)) #False Although it should be True
print(otv_points["0"](2)) #True Although it should be False
otv = {}
for i in range(5):
otv[str(i)] = lambda x: x == i
print(otv["0"](4)) #True Although it should be False
print(otv["0"](0)) #False Although it should be True
test = [0, 1, 2, 3, 4]
otv = {}
for i in range(5):
otv[str(i)] = lambda x: x == test[i]
print(otv["0"](4)) #True Although it should be False
print(otv["0"](0)) #False Although it should be True
Can you tell me how to get over this?