Indexing one-dimensional numpy.array as matrix

Posted by Alain on Stack Overflow See other posts from Stack Overflow or by Alain
Published on 2011-01-17T18:45:15Z Indexed on 2011/01/17 19:53 UTC
Read the original article Hit count: 151

Filed under:
|

I am trying to index a numpy.array with varying dimensions during runtime. To retrieve e.g. the first row of a n*m array a, you can simply do

a[0,:]

However, in case a happens to be a 1xn vector, this code above returns an index error:

IndexError: too many indices

As the code needs to be executed as efficiently as possible I don't want to introduce an if statement. Does anybody have a convenient solution that ideally doesn't involve changing any data structure types?

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy