问题:python:如何识别变量是数组还是标量
我有一个接受参数的函数NBins
。我想用标量50
或数组对此函数进行调用[0, 10, 20, 30]
。我如何识别函数的长度NBins
是多少?或换句话说,如果它是标量或向量?
我尝试了这个:
>>> N=[2,3,5]
>>> P = 5
>>> len(N)
3
>>> len(P)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
>>>
正如你看到的,我不能申请len
到P
,因为它不是一个数组….有什么样isarray
或isscalar
在Python?
谢谢
I have a function that takes the argument NBins
. I want to make a call to this function with a scalar 50
or an array [0, 10, 20, 30]
. How can I identify within the function, what the length of NBins
is? or said differently, if it is a scalar or a vector?
I tried this:
>>> N=[2,3,5]
>>> P = 5
>>> len(N)
3
>>> len(P)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
>>>
As you see, I can’t apply len
to P
, since it’s not an array…. Is there something like isarray
or isscalar
in python?
thanks
回答 0
>>> isinstance([0, 10, 20, 30], list)
True
>>> isinstance(50, list)
False
要支持任何类型的序列,请选中collections.Sequence
而不是list
。
注意:isinstance
还支持一个元组类,type(x) in (..., ...)
应避免检查,这是不必要的。
您可能还想检查 not isinstance(x, (str, unicode))
>>> isinstance([0, 10, 20, 30], list)
True
>>> isinstance(50, list)
False
To support any type of sequence, check collections.Sequence
instead of list
.
note: isinstance
also supports a tuple of classes, check type(x) in (..., ...)
should be avoided and is unnecessary.
You may also wanna check not isinstance(x, (str, unicode))
回答 1
先前的答案假定该数组是python标准列表。作为经常使用numpy的人,我建议使用以下Python测试:
if hasattr(N, "__len__")
Previous answers assume that the array is a python standard list. As someone who uses numpy often, I’d recommend a very pythonic test of:
if hasattr(N, "__len__")
回答 2
将@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))
为什么要用这种方法isinstance
而不是type(P)
与目标值进行比较?这是一个示例,我们制作并研究NewList
list的一个琐碎子类的行为。
>>> 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)
<class '__main__.NewList'>
>>> 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
以相同的方式。
Combining @jamylak and @jpaddison3’s answers together, if you need to be robust against numpy arrays as the input and handle them in the same way as lists, you should use
import numpy as np
isinstance(P, (list, tuple, np.ndarray))
This is robust against subclasses of list, tuple and numpy arrays.
And if you want to be robust against all other subclasses of sequence as well (not just list and tuple), use
import collections
import numpy as np
isinstance(P, (collections.Sequence, np.ndarray))
Why should you do things this way with isinstance
and not compare type(P)
with a target value? Here is an example, where we make and study the behaviour of NewList
, a trivial subclass of list.
>>> 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)
<class '__main__.NewList'>
>>> type(x) is list
False
>>> type(y) is list
True
>>> type(x).__name__
'NewList'
>>> isinstance(x, list)
True
Despite x
and y
comparing as equal, handling them by type
would result in different behaviour. However, since x
is an instance of a subclass of list
, using isinstance(x,list)
gives the desired behaviour and treats x
and y
in the same manner.
回答 3
numpy中有与isscalar()等效的东西吗?是。
>>> np.isscalar(3.1)
True
>>> np.isscalar([3.1])
False
>>> np.isscalar(False)
True
Is there an equivalent to isscalar() in numpy? Yes.
>>> np.isscalar(3.1)
True
>>> np.isscalar([3.1])
False
>>> np.isscalar(False)
True
回答 4
虽然@jamylak的方法更好,但这是另一种方法
>>> N=[2,3,5]
>>> P = 5
>>> type(P) in (tuple, list)
False
>>> type(N) in (tuple, list)
True
While, @jamylak’s approach is the better one, here is an alternative approach
>>> N=[2,3,5]
>>> P = 5
>>> type(P) in (tuple, list)
False
>>> type(N) in (tuple, list)
True
回答 5
另一种替代方法(使用类名属性):
N = [2,3,5]
P = 5
type(N).__name__ == 'list'
True
type(P).__name__ == 'int'
True
type(N).__name__ in ('list', 'tuple')
True
无需导入任何东西。
Another alternative approach (use of class name property):
N = [2,3,5]
P = 5
type(N).__name__ == 'list'
True
type(P).__name__ == 'int'
True
type(N).__name__ in ('list', 'tuple')
True
No need to import anything.
回答 6
这是我找到的最佳方法:检查__len__
和的存在__getitem__
。
您可能会问为什么?原因包括:
- 该流行方法
isinstance(obj, abc.Sequence)
在某些对象(包括PyTorch的Tensor)上失败,因为它们未实现__contains__
。 - 不幸的是,Python的collections.abc中没有任何东西可以检查
__len__
并且__getitem__
我认为这是处理类似数组对象的最小方法。 - 它适用于列表,元组,ndarray,Tensor等。
因此,事不宜迟:
def is_array_like(obj, string_is_array=False, tuple_is_array=True):
result = hasattr(obj, "__len__") and hasattr(obj, '__getitem__')
if result and not string_is_array and isinstance(obj, (str, abc.ByteString)):
result = False
if result and not tuple_is_array and isinstance(obj, tuple):
result = False
return result
请注意,我添加了默认参数,因为大多数时候您可能希望将字符串视为值,而不是数组。元组也是如此。
Here is the best approach I have found: Check existence of __len__
and __getitem__
.
You may ask why? The reasons includes:
- The popular method
isinstance(obj, abc.Sequence)
fails on some objects including PyTorch’s Tensor because they do not implement __contains__
. - Unfortunately, there is nothing in Python’s collections.abc that checks for only
__len__
and __getitem__
which I feel are minimal methods for array-like objects. - It works on list, tuple, ndarray, Tensor etc.
So without further ado:
def is_array_like(obj, string_is_array=False, tuple_is_array=True):
result = hasattr(obj, "__len__") and hasattr(obj, '__getitem__')
if result and not string_is_array and isinstance(obj, (str, abc.ByteString)):
result = False
if result and not tuple_is_array and isinstance(obj, tuple):
result = False
return result
Note that I’ve added default parameters because most of the time you might want to consider strings as values, not arrays. Similarly for tuples.
回答 7
>>> N=[2,3,5]
>>> P = 5
>>> type(P)==type(0)
True
>>> type([1,2])==type(N)
True
>>> type(P)==type([1,2])
False
>>> N=[2,3,5]
>>> P = 5
>>> type(P)==type(0)
True
>>> type([1,2])==type(N)
True
>>> type(P)==type([1,2])
False
回答 8
您可以检查变量的数据类型。
N = [2,3,5]
P = 5
type(P)
它将以P的数据类型输出。
<type 'int'>
这样就可以区分它是整数还是数组。
You can check data type of variable.
N = [2,3,5]
P = 5
type(P)
It will give you out put as data type of P.
<type 'int'>
So that you can differentiate that it is an integer or an array.
回答 9
令我惊讶的是,这样的基本问题似乎在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)
<ipython-input-40-f5055b93f729> in <module>()
----> 1 len(np.array(a))
TypeError: len() of unsized object
I am surprised that such a basic question doesn’t seem to have an immediate answer in python. It seems to me that nearly all proposed answers use some kind of type checking, that is usually not advised in python and they seem restricted to a specific case (they fail with different numerical types or generic iteratable objects that are not tuples or lists).
For me, what works better is importing numpy and using array.size, for example:
>>> 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
Note also:
>>> len(np.array([1,2]))
Out[5]: 2
but:
>>> len(np.array(a))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-40-f5055b93f729> in <module>()
----> 1 len(np.array(a))
TypeError: len() of unsized object
回答 10
只需使用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
Simply use size
instead of 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
回答 11
preds_test [0]的形状为(128,128,1),让我们使用isinstance()函数检查其数据类型isinstance接受2个参数。第一个参数是数据第二个参数是数据类型isinstance(preds_test [0],np.ndarray)给出Output为True。这意味着preds_test [0]是一个数组。
preds_test[0] is of shape (128,128,1) Lets check its data type using isinstance() function isinstance takes 2 arguments. 1st argument is data 2nd argument is data type isinstance(preds_test[0], np.ndarray) gives Output as True. It means preds_test[0] is an array.
回答 12
为了回答标题中的问题,判断变量是否为标量的直接方法是尝试将其转换为浮点数。如果得到TypeError
,则不是。
N = [1, 2, 3]
try:
float(N)
except TypeError:
print('it is not a scalar')
else:
print('it is a scalar')
To answer the question in the title, a direct way to tell if a variable is a scalar is to try to convert it to a float. If you get TypeError
, it’s not.
N = [1, 2, 3]
try:
float(N)
except TypeError:
print('it is not a scalar')
else:
print('it is a scalar')
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。