2012-05-28 39 views
13

如何確定Numpy數組是否包含字符串?在Numpy:檢查數組的字符串數據類型

a = np.array('hi world') 

陣列a具有數據類型dtype('|S8'),其中8指在字符串中的字符數。

我看不到正則表達式(如re.match('\|S\d+', a.dtype))在這裏如何工作,因爲數據類型不僅僅是'|S8'

乾杯, 尼科

回答

15
a.dtype.char == 'S' 

a.dtype.type is np.string_ 

NumPy docs, Data type objects, Attributes

+1

我注意到還有a.dtype.kind =='S'。甜! –

+0

只是一個註釋:似乎這需要在Python 3.x中爲'a.dtype.type爲np.str_' –

+0

對於Python 2.x _and_ 3.x,您可以在{'中測試'a.dtype.kind' U','S'}'來抓住字符串和unicode。 – Chipmuenk