简体   繁体   English

在 python 中将字符串转换为 float16

[英]convert string to float16 in python

I am currently using numpy (I don't know how to make a datatype float16 in python otherwise) as this:我目前正在使用 numpy(否则我不知道如何在 python 中创建数据类型 float16),如下所示:

a = np.string_('1.234').astype(np.float16)

then:然后:

type(a)
numpy.float16

Is ther another option?还有其他选择吗? Is it better?好点吗?

It would be great if you could explain what am I doing when converting the python string into numpy string!如果您能解释一下将 python 字符串转换为 numpy 字符串时我在做什么,那就太好了!

Thank you!谢谢你!

You can go directly from python string to np.float16 :您可以 go 直接从 python 字符串到np.float16

import numpy as np

f16 = np.float16("1.234")
print(type(f16))

f16 = np.float16(1.234)  # works as well

Output: Output:

<class 'numpy.float16'>

Reference: https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.inexact参考: https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.inexact

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

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