2012-05-28 40 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