3

I have an np.array with 3 dimension. I just want to select all n-th element from the last array.

I know that I can select it like

array[:, :, 0]

But I need to use a function, is there any numpy function to do that ?

3
  • What do you mean by you needing to use a function? What part is variable? Commented Jul 7, 2019 at 20:52
  • 1
    I'm not using numpy through python I just play with PythonObject so I can't use [] like in python but I can call Python function so if I can select with a function that would help me Commented Jul 7, 2019 at 20:54
  • 1
    You create a function which takes a numpy array as input and returns the required element. Commented Jul 7, 2019 at 20:56

1 Answer 1

4

The [] operator calls __getitem__ on the object it was used on. In your case, it would be the equivalent of calling

array.__getitem__((slice(None), slice(None), 0))

This is because [:] is an empty slice (i.e. slice(None)), and you are calling the [] operator with 3 value, which is equivalent to calling __getitem__ with a tuple of 3 values.

Sign up to request clarification or add additional context in comments.

4 Comments

It's exactly what I was searching ! instead of using [] I needed to use a function
Well, fair play, I didn't see this being the answer but you've clearly seen the question in a different way to me.
@roganjosh They're interfacing with Numpy/Python from another language (as evident by their comment on the question), and so they can't use Python syntax to access elements in their array. They were looking for a function they could call in-place of the Python [] operator.
That makes sense. You've already got my upvote but I wouldn't have snagged this approach from what was said tbh so thanks for pointing it out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.