将多维数组字符串转换为python中的float

时间:2015-07-29 08:37:34

标签: python string numpy multidimensional-array integer

我有一个像这样的多维数组:

True

并且所有值都是您看到的字符串。 我需要将它们转换为浮动值,还有一种方法吗? 我找到了转换单个字符串的解决方案,但它对数组不起作用。

2 个答案:

答案 0 :(得分:4)

您可以在列表理解中使用map

>>> li=[['120', '29.9475077984', '1'],
...        ['125', '31.3887667742', '1'],
...        ['125', '32.3881706091', '1'],
...        ['125', '34.4894481007', '1'],
...        ['126', '36.1494551046', '1'],
...        ['127', '39.3121447948', '1'],
...        ['128', '43.8203811171', '1'],
...        ['128', '49.3179066095', '1'],
...        ['128', '53.4929489926', '1'],
...        ['128', '55.1837748899', '1'],
...        ['130', '55.9167038553', '1'],
...        ['130', '56.2727376481', '1'],
...        ['130', '57.480058071', '1'],
...        ['130', '60.3922465138', '1'],
...        ['130', '61.2506277637', '1'],
...        ['130', '60.5279054759', '1'],
...        ['143', '62.139526711', '1'],
...        ['143', '65.4147315349', '1'],
...        ['143', '72.3278873965', '1']]
>>> 
>>> 
>>> [map(float,i) for i in li]
[[120.0, 29.9475077984, 1.0], [125.0, 31.3887667742, 1.0], [125.0, 32.3881706091, 1.0], [125.0, 34.4894481007, 1.0], [126.0, 36.1494551046, 1.0], [127.0, 39.3121447948, 1.0], [128.0, 43.8203811171, 1.0], [128.0, 49.3179066095, 1.0], [128.0, 53.4929489926, 1.0], [128.0, 55.1837748899, 1.0], [130.0, 55.9167038553, 1.0], [130.0, 56.2727376481, 1.0], [130.0, 57.480058071, 1.0], [130.0, 60.3922465138, 1.0], [130.0, 61.2506277637, 1.0], [130.0, 60.5279054759, 1.0], [143.0, 62.139526711, 1.0], [143.0, 65.4147315349, 1.0], [143.0, 72.3278873965, 1.0]]

如果你有一个numpy数组,你可以使用np.astype

>>> li.astype(float)
array([[ 120.        ,   29.9475078 ,    1.        ],
       [ 125.        ,   31.38876677,    1.        ],
       [ 125.        ,   32.38817061,    1.        ],
       [ 125.        ,   34.4894481 ,    1.        ],
       [ 126.        ,   36.1494551 ,    1.        ],
       [ 127.        ,   39.31214479,    1.        ],
       [ 128.        ,   43.82038112,    1.        ],
       [ 128.        ,   49.31790661,    1.        ],
       [ 128.        ,   53.49294899,    1.        ],
       [ 128.        ,   55.18377489,    1.        ],
       [ 130.        ,   55.91670386,    1.        ],
       [ 130.        ,   56.27273765,    1.        ],
       [ 130.        ,   57.48005807,    1.        ],
       [ 130.        ,   60.39224651,    1.        ],
       [ 130.        ,   61.25062776,    1.        ],
       [ 130.        ,   60.52790548,    1.        ],
       [ 143.        ,   62.13952671,    1.        ],
       [ 143.        ,   65.41473153,    1.        ],
       [ 143.        ,   72.3278874 ,    1.        ]])

答案 1 :(得分:1)

您可以使用np.ndarray.astype()功能 -

示例 -

In [5]: n= np.array([['120', '29.9475077984', '1'],
   ...:        ['125', '31.3887667742', '1'],
   ...:        ['125', '32.3881706091', '1'],
   ...:        ['125', '34.4894481007', '1'],
   ...:        ['126', '36.1494551046', '1'],
   ...:        ['127', '39.3121447948', '1'],
   ...:        ['128', '43.8203811171', '1'],
   ...:        ['128', '49.3179066095', '1'],
   ...:        ['128', '53.4929489926', '1'],
   ...:        ['128', '55.1837748899', '1'],
   ...:        ['130', '55.9167038553', '1'],
   ...:        ['130', '56.2727376481', '1'],
   ...:        ['130', '57.480058071', '1'],
   ...:        ['130', '60.3922465138', '1'],
   ...:        ['130', '61.2506277637', '1'],
   ...:        ['130', '60.5279054759', '1'],
   ...:        ['143', '62.139526711', '1'],
   ...:        ['143', '65.4147315349', '1'],
   ...:        ['143', '72.3278873965', '1']])

In [8]: n = n.astype(np.float)

In [9]: n
Out[9]:
array([[ 120.        ,   29.9475078 ,    1.        ],
       [ 125.        ,   31.38876677,    1.        ],
       [ 125.        ,   32.38817061,    1.        ],
       [ 125.        ,   34.4894481 ,    1.        ],
       [ 126.        ,   36.1494551 ,    1.        ],
       [ 127.        ,   39.31214479,    1.        ],
       [ 128.        ,   43.82038112,    1.        ],
       [ 128.        ,   49.31790661,    1.        ],
       [ 128.        ,   53.49294899,    1.        ],
       [ 128.        ,   55.18377489,    1.        ],
       [ 130.        ,   55.91670386,    1.        ],
       [ 130.        ,   56.27273765,    1.        ],
       [ 130.        ,   57.48005807,    1.        ],
       [ 130.        ,   60.39224651,    1.        ],
       [ 130.        ,   61.25062776,    1.        ],
       [ 130.        ,   60.52790548,    1.        ],
       [ 143.        ,   62.13952671,    1.        ],
       [ 143.        ,   65.41473153,    1.        ],
       [ 143.        ,   72.3278874 ,    1.        ]])