python:如何識別變數是陣列還是標量 - python: how to identify if a variable is an array or a scalar

語言: CN / TW / HK

問題:

I have a function that takes the argument NBins .我有一個接受引數NBins的函式。 I want to make a call to this function with a scalar 50 or an array [0, 10, 20, 30] .我想使用標量50或陣列[0, 10, 20, 30]呼叫此函式。 How can I identify within the function, what the length of NBins is?如何在函式內識別NBins的長度是多少? 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?如您所見,我不能將len應用於P ,因為它不是陣列....python 中是否有類似isarrayisscalar東西?

thanks謝謝


解決方案:

參考一: http://stackoom.com/question/18WGp
參考二: python: how to identify if a variable is an array or a scalar
「其他文章」