Chris - 7 months ago 29

Python Question

I have one numpy array with a N dimensions where N can vary:

`arr1 = np.array([0,0,1,0])`

arr2 = np.array([[0,0],[0,0],[0,1],[0,0]])

I need to be able to pass an array and tuple/int and get the integer in that location returned:

`assert myfunc(arr1, 2) == 1`

assert myfunc(arr2, (2, 1)) == 1

Answer

I'm not sure I understood your question correctly; but, you can directly use tuple as an index to retrieve the specific elements.

For instance;

```
arr1 = np.array([0,0,1,0])
arr2 = np.array([[0,0],[0,0],[0,1],[0,0]])
arr3 = np.array([[[0,0],[0,0],[0,1],[0,0]], [[0,0],[0,0],[0,1],[0,0]]])
assert arr1[2] == 1
assert arr2[(2, 1)] == 1
assert arr3[(1,2,1)] == 0
```