本文簡要介紹 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
