擅长:python、mysql、java
<p>可以使用<a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.genfromtxt.html" rel="nofollow noreferrer">^{<cd1>}</a>将字符串数组转换为浮点数组(带NaNs):</p>
<pre><code>In [83]: np.set_printoptions(precision=3, suppress=True)
In [84]: np.genfromtxt(np.array(['1','2','3.14','1e-3','b','nan','inf','-inf']))
Out[84]: array([ 1. , 2. , 3.14 , 0.001, nan, nan, inf, -inf])
</code></pre>
<hr/>
<p>以下是识别“数字”字符串的方法:</p>
<pre><code>In [34]: x
Out[34]:
array(['1', '2', 'a'],
dtype='|S1')
In [35]: x.astype('unicode')
Out[35]:
array([u'1', u'2', u'a'],
dtype='<U1')
In [36]: np.char.isnumeric(x.astype('unicode'))
Out[36]: array([ True, True, False], dtype=bool)
</code></pre>
<p>注意,“numeric”是指只包含数字字符的unicode,即具有unicode数值属性的字符。它不包括小数点。因此<code>u'1.3'</code>不被视为“数字”。</p>