我有一个这样的多维数组:
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'],
和所有的值都是字符串。我需要把它们转换成浮点值,有办法吗?我找到了一个转换单个字符串的解决方案,但它不适用于数组。
您可以在列表推导中使用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. ]])
可以使用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. ]])