本文简要介绍 python 语言中 numpy.isscalar 的用法。
用法:
numpy.isscalar(element)如果类型返回 True元素是标量类型。
- element: 任何
输入参数,可以是任何类型和形状。
- val: bool
如果元素是标量类型则为 True,否则为 False。
参数:
返回:
注意:
如果您需要更严格的方法来识别数值标量,使用
isinstance(x, numbers.Number),因为返回False对于大多数非数字元素(例如字符串)。在大多数情况下,应该使用
np.ndim(x) == 0而不是这个函数,因为它也会为 0d 数组返回 true。这就是 numpy 以gradient的dx参数和histogram的bins参数的样式重载函数的方式。一些关键区别:x
isscalar(x)np.ndim(x) == 0PEP 3141 数字对象(包括内置函数)
TrueTrue内置字符串和缓冲区对象
TrueTrue其他内置对象,例如
pathlib.Path,例外, 的结果re.compileFalseTrue第三方对象,如
matplotlib.figure.FigureFalseTrue零维 numpy 数组
FalseTrue其他 numpy 数组
FalseFalse列表、元组和其他序列对象
FalseFalse例子:
>>> np.isscalar(3.1) True >>> np.isscalar(np.array(3.1)) False >>> np.isscalar([3.1]) False >>> np.isscalar(False) True >>> np.isscalar('numpy') TrueNumPy 支持 PEP 3141 号码:
>>> from fractions import Fraction >>> np.isscalar(Fraction(5, 17)) True >>> from numbers import Number >>> np.isscalar(Number()) True
相关用法
- Python numpy issctype用法及代码示例
- Python numpy issubdtype用法及代码示例
- Python numpy issubclass_用法及代码示例
- Python numpy issubsctype用法及代码示例
- Python numpy isclose用法及代码示例
- Python numpy isnat用法及代码示例
- Python numpy is_busday用法及代码示例
- Python numpy isposinf用法及代码示例
- Python numpy iscomplexobj用法及代码示例
- Python numpy isfinite用法及代码示例
- Python numpy iscomplex用法及代码示例
- Python numpy isin用法及代码示例
- Python numpy isinf用法及代码示例
- Python numpy isrealobj用法及代码示例
- Python numpy isneginf用法及代码示例
- Python numpy isreal用法及代码示例
- Python numpy isnan用法及代码示例
- Python numpy isfortran用法及代码示例
- Python numpy interp用法及代码示例
- Python numpy iinfo用法及代码示例
- Python numpy in1d用法及代码示例
- Python numpy indices用法及代码示例
- Python numpy ix_用法及代码示例
- Python numpy imag用法及代码示例
- Python numpy insert用法及代码示例
注:本文由纯净天空筛选整理自numpy.org大神的英文原创作品 numpy.isscalar。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
