python:如何識別變數是陣列還是標量 - python: how to identify if a variable is an array or a scalar
問題:
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 中是否有類似isarray
或isscalar
東西?
thanks謝謝
解決方案:
參考一: http://stackoom.com/question/18WGp參考二: python: how to identify if a variable is an array or a scalar
「其他文章」
- 如何在 JavaScript 中編寫內聯 IF 語句? - How to write an inline IF statement in JavaScript?
- 如何限制 Parallel.ForEach? - How can I limit Parallel.ForEach?
- python:如何識別變數是陣列還是標量 - python: how to identify if a variable is an array or a scalar
- 使用 Windows 命令提示符安裝 Windows 服務? - Install a Windows service using a Windows command prompt?
- 如何在沒有我自己的登錄檔的情況下安裝私有 NPM 模組? - How to install a private NPM module without my own registry?
- 如何使用 pip 更新/升級包? - How to update/upgrade a package using pip?
- 條件使用NOT NIL的Rails - Rails where condition using NOT NIL
- 像屬性一樣訪問字典鍵? - Accessing dict keys like an attribute?
- 檔案輸入“接受”屬性 - 有用嗎? - File input 'accept' attribute - is it useful?
- Python while 語句上的 Else 子句 - Else clause on Python while statement
- 正則表示式匹配兩個單詞之一 - Regex match one of two words
- 我們如何下載 blob url 影片 [關閉] - How do we download a blob url video [closed]
- 比較兩個字串,忽略 C# 中的大小寫 [重複] - Comparing two strings, ignoring case in C# [duplicate]
- Java 介面/實現命名約定 [重複] - Java Interfaces/Implementation naming convention [duplicate]
- 如何檢查是否在 Cygwin、Mac 或 Linux 中執行? - How to check if running in Cygwin, Mac or Linux?
- Java 日期與日曆 - Java Date vs Calendar
- 在 JavaScript 中將日期轉換為另一個時區 - Convert date to another timezone in JavaScript
- 為SQL Server Express 2012啟用遠端連線 - Enable remote connections for SQL Server Express 2012
- 如何從程式碼中獲取裝置的IP地址? - How to get IP address of the device from code?
- 獲取簡短的 Git 版本雜湊 - Get the short Git version hash