python:如何识别变量是数组还是s
我有一个带参数len的函数。我想用标量P或数组isarray调用此函数。如何在函数内识别isscalar的长度是多少? 或者换句话说,如果它是标量或向量?
我试过这个:
>>> N=[2,3,5]
>>> P = 5
>>> len(N)
3
>>> len(P)
Traceback (most recent call last):
File "", line 1, in
TypeError: object of type 'int' has no len()
>>>
如你所见,我无法申请len至P,因为它不是数组....在python中有类似isarray或isscalar的内容吗?
谢谢
10个解决方案
279 votes
>>> isinstance([0, 10, 20, 30], list)
True
>>> isinstance(50, list)
False
要支持任何类型的序列,请检查not isinstance(x, (str, unicode))而不是type(x) in (..., ...)。
注意:not isinstance(x, (str, unicode))也支持一个类的元组,应该避免检查type(x) in (..., ...)是不必要的。
您可能还想查看not isinstance(x, (str, unicode))
jamylak answered 2019-03-05T14:20:11Z
94 votes
以前的答案假设该数组是python标准列表。 作为经常使用numpy的人,我推荐一个非常pythonic测试:
if hasattr(N, "__len__")
jpaddison3 answered 2019-03-05T14:20:40Z
31 votes
将@jamylak和@jpaddison3的答案结合在一起,如果你需要对numpy数组作为输入强大并以与列表相同的方式处理它们,你应该使用
import numpy as np
isinstance(P, (list, tuple, np.ndarray))
这对list,tuple和numpy数组的子类很有用。
如果你想要对所有其他序列子类(不仅仅是列表和元组)具有健壮性,请使用
import collections
import numpy as np
isinstance(P, (collections.Sequence, np.ndarray))
你为什么要用x这样做,而不是用目标值比较y? 这是一个例子,我们制作和研究type的行为,这是一个简单的列表子类。
>>> class NewList(list):
... isThisAList = '???'
...
>>> x = NewList([0,1])
>>> y = list([0,1])
>>> print x
[0, 1]
>>> print y
[0, 1]
>>> x==y
True
>>> type(x)
>>> type(x) is list
False
>>> type(y) is list
True
>>> type(x).__name__
'NewList'
>>> isinstance(x, list)
True
尽管x和y相等,但处理它们会导致type不同的行为。 但是,由于x是子类list的实例,因此使用isinstance(x,list)给出了所需的行为,并以相同的方式处理x和y。
scottclowe answered 2019-03-05T14:21:41Z
22 votes
numpy中是否有等同于isscalar()的东西? 是。
>>> np.isscalar(3.1)
True
>>> np.isscalar([3.1])
False
>>> np.isscalar(False)
True
jmhl answered 2019-03-05T14:22:11Z
14 votes
虽然@ jamylak的方法更好,但这是一种替代方法
>>> N=[2,3,5]
>>> P = 5
>>> type(P) in (tuple, list)
False
>>> type(N) in (tuple, list)
True
Sukrit Kalra answered 2019-03-05T14:22:50Z
3 votes
另一种替代方法(使用类名属性):
N = [2,3,5]
P = 5
type(N).__name__ == 'list'
True
type(P).__name__ == 'int'
True
type(N).__name__ in ('list', 'tuple')
True
无需进口任何东西。
Marek answered 2019-03-05T14:23:32Z
3 votes
只需使用size而不是len!
>>> from numpy import size
>>> N = [2, 3, 5]
>>> size(N)
3
>>> N = array([2, 3, 5])
>>> size(N)
3
>>> P = 5
>>> size(P)
1
Mathieu Villion answered 2019-03-05T14:24:03Z
2 votes
>>> N=[2,3,5]
>>> P = 5
>>> type(P)==type(0)
True
>>> type([1,2])==type(N)
True
>>> type(P)==type([1,2])
False
simple_human answered 2019-03-05T14:24:30Z
2 votes
您可以检查变量的数据类型。
N = [2,3,5]
P = 5
type(P)
它会给你输出作为P的数据类型。
这样您就可以区分它是整数还是数组。
unnati patil answered 2019-03-05T14:25:17Z
1 votes
我很惊讶这样的基本问题似乎没有在python中立即得到答案。在我看来,几乎所有提议的答案都使用某种类型检查,通常不在python中建议,它们似乎仅限于特定情况(它们使用不同的数字类型或不是元组或列表的通用可迭代对象失败)。
对我来说,更好的是导入numpy并使用array.size,例如:
>>> a=1
>>> np.array(a)
Out[1]: array(1)
>>> np.array(a).size
Out[2]: 1
>>> np.array([1,2]).size
Out[3]: 2
>>> np.array('125')
Out[4]: 1
另请注意:
>>> len(np.array([1,2]))
Out[5]: 2
但:
>>> len(np.array(a))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 len(np.array(a))
TypeError: len() of unsized object
Vincenzooo answered 2019-03-05T14:26:06Z