numpy.
isscalar
如果类型为 element 是标量类型。
输入参数,可以是任何类型和形状。
如果真 element 是标量类型,如果不是,则为false。
参见
ndim
获取数组的维数
笔记
如果你需要一个更严格的方法来确定 数字的 标量,使用 isinstance(x, numbers.Number) ,当它返回时 False 对于大多数非数字元素,如字符串。
isinstance(x, numbers.Number)
False
在大多数情况下 np.ndim(x) == 0 应该使用而不是此函数,因为对于0d数组,这也将返回true。这就是numpy重载如何以 dx 论据 gradient 以及 bins 参数 histogram . 一些关键区别:
np.ndim(x) == 0
dx
gradient
bins
histogram
X
isscalar(x)
PEP 3141数字对象(包括内置)
True
内置字符串和缓冲区对象
其他内置对象,如 pathlib.Path , Exception 的结果 re.compile
pathlib.Path
re.compile
第三方对象,如 matplotlib.figure.Figure
matplotlib.figure.Figure
零维numpy数组
其他numpy数组
list , tuple 和其他序列对象
实例
>>> np.isscalar(3.1) True >>> np.isscalar(np.array(3.1)) False >>> np.isscalar([3.1]) False >>> np.isscalar(False) True >>> np.isscalar('numpy') True
Numpy支持PEP 3141编号:
>>> from fractions import Fraction >>> np.isscalar(Fraction(5, 17)) True >>> from numbers import Number >>> np.isscalar(Number()) True