我有一个字符串如下:
>>> a
'0 911 872.9 354.9 901.9 395.0 904.6 414.0 903.8 400.5'
现在我想把它转换成一个数组:
>>> b
array([ 0. , 911. , 872.9, 354.9, 901.9, 395. , 904.6, 414. ,
903.8, 400.5])
最符合 Pythonic 的方法是什么?
请您参考如下方法:
使用numpy.fromstring :
import numpy as np
np.fromstring(a, dtype=float, sep=' ')
演示:
>>> np.fromstring('0 911 872.9 354.9 901.9 395.0 904.6 414.0 903.8 400.5', dtype=float, sep=' ')
array([ 0. , 911. , 872.9, 354.9, 901.9, 395. , 904.6, 414. , 903.8, 400.5])
