How detect length of a numpy array with only one element?
Posted
by
mishaF
on Stack Overflow
See other posts from Stack Overflow
or by mishaF
Published on 2010-12-30T19:48:36Z
Indexed on
2010/12/30
19:54 UTC
Read the original article
Hit count: 163
I am reading in a file using numpy.genfromtxt which brings in columns of both strings and numeric values. One thing I need to do is detect the length of the input. This is all fine provided there are more than one value read into each array.
But...if there is only one element in the resulting array, the logic fails. I can recreate an example here:
import numpy as np
a = np.array(2.3)
len(a) returns an error saying:
TypeError: len() of unsized object
however, If a has 2 or more elements, len() behaves as one would expect.
import numpy as np
a = np.array([2.3,3.6])
len(a) returns 2
My concern here is, if I use some strange exception handling, I can't distinguish between a being empty and a having length = 1.
© Stack Overflow or respective owner