他们似乎给了我相同的结果:
In [32]: s
Out[32]: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
In [27]: np.frombuffer(s, dtype="int8")
Out[27]:
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
In [28]: np.fromstring(s, dtype="int8")
Out[28]:
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
In [33]: b = buffer(s)
In [34]: b
Out[34]: <read-only buffer for 0x035F8020, size -1, offset 0 at 0x036F13A0>
In [35]: np.fromstring(b, dtype="int8")
Out[35]:
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
In [36]: np.frombuffer(b, dtype="int8")
Out[36]:
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
Run Code Online (Sandbox Code Playgroud)
何时应该使用另一个?
Joe*_*ton 31
从实际角度来看,不同之处在于:
x = np.fromstring(s, dtype='int8')
Run Code Online (Sandbox Code Playgroud)
将在内存中复制字符串,同时:
x = np.frombuffer(s, dtype='int8')
Run Code Online (Sandbox Code Playgroud)
要么
x = np.frombuffer(buffer(s), dtype='int8')
Run Code Online (Sandbox Code Playgroud)
将直接使用字符串的内存缓冲区,不会使用任何*额外的内存.frombuffer如果输入为buffer字符串,则using 也将导致只读数组,因为字符串在python中是不可变的.
(*忽略用于附加python ndarray对象的几个字节的内存- 将共享数据的底层内存.)
如果您不熟悉buffer对象(memoryview在python3.x中),它们本质上是C级库公开内存块以供在python中使用的一种方式.它基本上是一个用于托管访问原始内存的python接口.
如果你正在使用暴露缓冲区接口的东西,那么你可能想要使用frombuffer.(Python 2.x字符串和python 3.x bytes公开缓冲区接口,但是你将获得一个只读数组,因为python字符串是不可变的.)
否则,用于fromstring从字符串创建numpy数组.(除非你知道你在做什么,并且想要严格控制内存使用等)
| 归档时间: |
|
| 查看次数: |
15274 次 |
| 最近记录: |