Skip to content Skip to sidebar Skip to footer

How To Index 0-d Array In Python?

This may be a well-known question stored in some FAQ but i can't google the solution. I'm trying to write a scalar function of scalar argument but allowing for ndarray argument. Th

Solution 1:

This will work fine in NumPy >= 1.9 (not released as of writing this). On previous versions you can work around by an extra np.asarray call:

x[np.asarray(x > 0)] = 0

Solution 2:

Could you call f([1.0]) instead?

Otherwise you can do:

x = np.asarray(x)
if x.ndim == 0:
    x = x[..., None]

Post a Comment for "How To Index 0-d Array In Python?"