1

Why does NumPy allow array[row_index, ] but array[, col_index] is not valid and gives Syntax Error. e.g. if I want to traverse the array row wise NumPy.array[row_index, :] and NumPy.array[row_index, ] both give the same answer where as only NumPy.array[:, col_index] produces the result in the latter case. Is there any reason behind this that I'm missing?

0

1 Answer 1

2

arr[idx,] is actually short for arr[(idx,)], passing a tuple to the __getitem__ method. In python a comma creates a tuple (in most circumstances). (1) is just 1, (1,) is a one element tuple, as is 1,.

arr[,idx] is gives a syntax error. That's the interpreter complaining, not numpy.

arr[3], arr[3,] and arr[3,:] are all the same for a 2d array. Trailing : are added as needed. Leading : have to be explicit.

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

Comments

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.