Is there a way in Python to let the len(x)
function (or any similar function) return 1 if x
is a simple float?
In my case, I have x
as a input parameter to a function, and I want to make it robust to (NumPy) array type inputs of x
as well as simple scalar float input of x
. In the function, the len(x)
function is used, but it throws an error object of type 'float' has no len()
if x
is a float, whereas I want it to return 1. Of course I can write an if statement, but I feel like there should be a shorter solution.
def myfunc(x):
y=np.zeros((5,len(x)))
y[1,:]=x
....
return y