1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python 字符串数组转换为浮点数 如何在numpy中将字符串数组转换为浮点数数组?...

python 字符串数组转换为浮点数 如何在numpy中将字符串数组转换为浮点数数组?...

时间:2022-07-17 00:22:17

相关推荐

python 字符串数组转换为浮点数 如何在numpy中将字符串数组转换为浮点数数组?...

How to convert

["1.1", "2.2", "3.2"]

to

[1.1, 2.2, 3.2]

in NumPy?

解决方案

Well, if you're reading the data in as a list, just do np.array(map(float, list_of_strings)) (or equivalently, use a list comprehension). (In Python 3, you'll need to call list on the map return value if you use map, since map returns an iterator now.)

However, if it's already a numpy array of strings, there's a better way. Use astype().

import numpy as np

x = np.array(['1.1', '2.2', '3.3'])

y = x.astype(np.float)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。