numpy 中是否有一个现有函数可以告诉我一个值是数字类型还是 numpy 数组?我正在编写一些需要处理几种不同表示形式的数字的数据处理代码(“数字”是指可以使用标准算术运算符 +、-、*、/、* 操作的数字量的任何表示形式) *)。
我正在寻找的一些行为示例
>>> is_numeric(5)
True
>>> is_numeric(123.345)
True
>>> is_numeric('123.345')
False
>>> is_numeric(decimal.Decimal('123.345'))
True
>>> is_numeric(True)
False
>>> is_numeric([1, 2, 3])
False
>>> is_numeric([1, '2', 3])
False
>>> a = numpy.array([1, 2.3, 4.5, 6.7, 8.9])
>>> is_numeric(a)
True
>>> is_numeric(a[0])
True
>>> is_numeric(a[1])
True
>>> is_numeric(numpy.array([numpy.array([1]), numpy.array([2])])
True
>>> is_numeric(numpy.array(['1'])
False
如果不存在这样的功能,我知道写一个应该不难,比如
isinstance(n, (int, float, decimal.Decimal, numpy.number, numpy.ndarray))
但是我应该在列表中包括其他数字类型吗?