欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

numpy使用fromstring创建矩阵的实例

时间:2021-07-23 07:39:35|栏目:Python代码|点击:

使用字符串创建矩阵是一个很实用的功能,之前自己尝试了很多次的小功能使用这个方法就能够简单实现。

创建长度为16的字符串,是为了方便能够在各种数据类型之间转换。

>>> s = "mytestfromstring"
>>> len(s)
16

这个功能其实是比较让我兴奋的一个小功能,因为这个简单的转换实现了ASCII码的转换

>>> np.fromstring(s,dtype=np.int8)
array([109, 121, 116, 101, 115, 116, 102, 114, 111, 109, 115, 116, 114,
    105, 110, 103], dtype=int8)
 
>>> np.fromstring(s,dtype=np.int16)
array([31085, 25972, 29811, 29286, 28015, 29811, 26994, 26478], dtype=int16)
>>> np.fromstring(s,dtype=np.int32)
array([1702132077, 1919317107, 1953721711, 1735289202], dtype=int32)
>>> np.fromstring(s,dtype=np.int64)
array([8243404206920464749, 7453010373645659503])
>>> np.fromstring(s,dtype=np.single)
array([ 7.21560833e+22,  4.56462917e+30,  7.71452462e+31,
     1.12586835e+24], dtype=float32)
>>> np.fromstring(s,dtype=np.float32)
array([ 7.21560833e+22,  4.56462917e+30,  7.71452462e+31,
     1.12586835e+24], dtype=float32)
>>> np.fromstring(s,dtype=np.float64)
array([ 1.19783602e+243,  1.69375610e+190])

float默认的数据宽度是64,我使用的是Mac,本身是64位的,倒不知是不是跟操作系统有关?

>>> np.fromstring(s,dtype=np.float)
array([ 1.19783602e+243,  1.69375610e+190])

上一篇:Python设计模式之职责链模式原理与用法实例分析

栏    目:Python代码

下一篇:python迭代器常见用法实例分析

本文标题:numpy使用fromstring创建矩阵的实例

本文地址:http://www.codeinn.net/misctech/160750.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有