Reputation: 333
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?
Upvotes: 1
Views: 194
Reputation: 231355
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.
Upvotes: 2