![]() |
VOOZH | about |
In this article, we will see how to get the index of the NumPy multidimensional array in reverse order.
Approach
Final Index= Total Elements In Rows - Current Index-1
Example:
Input:
[[1,2,3,4,2],
[2,3,4,1,5],
[2,2,4,3,2],
[1,3,4,2,4]]
Output:
[4 0 4 3]
Explanation: In the above example, we are trying to find the first occurrence indexing of '2' in reverse order and we had 5 elements in each row and after backward indexing we get an array like [0, 4, 0, 1] now using our formula above.
final_list[0] => Total Elements In Rows - Current Index- 1 => 5-0-1 =>4
final_list[1] => Total Elements In Rows - Current Index- 1 => 5-4-1 =>0
final_list[2] => Total Elements In Rows - Current Index- 1 => 5-0-1 =>4
final_list[3] => Total Elements In Rows - Current Index- 1 => 5-1-1 =>3
Example 1:
Output:
[4 0 4 3]
Example 2:
The above method could work for string as well. In the example below we are trying to find the first occurrence indexing of 'Sam' in reverse order.
Output:
[0 1 2 ]
Example 3:
For Boolean data, we have the same approach, but since it has only 0 or 1 as the value we can use argmax() which will find the index of the highest value (for each row with axis=1). Since True is equivalent to 1 and False to 0, it'll record the index of the first True value.
Output:
[3 2 3 3]