I've been stuck for quite some time on this problem.
I have an array of shape (4,3,3). In my real case, the shape is much bigger.
a = np.array([
[[1,2,3], [3,4,2], [1,3,4]],
[[1,2,3], [3,6,2], [1,4,4]],
[[1,2,3], [3,6,2], [1,4,4]],
[[1,2,3], [3,6,2], [1,2,4]]
])
I want to use the np.where function to replace some of the list inside the array.
For example, each time I have the list [1,2,3], I want to replace it by the list [99,99,99].
Without using np.where, to check if the list is completely equal to [1,2,3], I would use some function like all().
Using np.where:
np.where(a == [1,2,3], 99, a)
Will result in:
array([[[99, 99, 99],
[ 3, 4, 2],
[99, 3, 4]],
[[99, 99, 99],
[ 3, 6, 2],
[99, 4, 4]],
[[99, 99, 99],
[ 3, 6, 2],
[99, 4, 4]],
[[99, 99, 99],
[ 3, 6, 2],
[99, 99, 4]]])
As you can see on the 3rd row, a part of the list has been replaced by 99.
I only want to replace the full list (never a part of the list) when it's exactly [1,2,3] and in the right order.
But I cannot solve this problem. I've been looking a lot online, with no luck.
a[(a == [1,2,3]).all(-1)] = 99, IIUC. Please include your desired result.np.whereworks at the element level. I think you have to use.all()at some point to tell np to look at a whole row, now just a single element. @Michael's solution is very elegant imho. It doesn't get much more concise than that!