简体   繁体   English

将多维数组字符串转换为浮在python中

[英]Convert Multidimensional array string to float in python

I have a this multidimension array like this: 我有一个这样的多维数组:

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'],

and all the values are string as you see. 如您所见,所有值都是字符串。 I need to convert them in float values, there's a way to do it? 我需要将它们转换为浮点值,有办法吗? I've found a solution to convert a single string but it doesn't work for the array. 我找到了一种转换单个字符串的解决方案,但不适用于该数组。

You can use map in a list comprehension : 您可以在列表推导中使用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]]

And if you have a numpy array you can use np.astype : 如果您有一个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.        ]])

You can use np.ndarray.astype() function - 您可以使用np.ndarray.astype()函数-

Example - 范例-

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.        ]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM