python - 不删除尾随零的 NumPy 字节数组
问题描述
如何创建一个不删除尾随零的 NumPy 字节数组?np.bytes_
并np.str_
删除尾随零。np.void
要求数组的每个元素都具有相同的长度。是否有任何 dtype 可以包含带有尾随零和可变长度的字节块?
>>> np.array([b'\x00', b'\x01\x00'], np.void)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Invalid type promotion with void datatypes of different lengths. Use the `np.bytes_` datatype instead to pad the shorter value with trailing zero bytes.
>>> np.array([b'\x00', b'\x01\x00'], np.bytes_)
array([b'', b'\x01'], dtype='|S2')
>>> np.array([b'\x00', b'\x01\x00'], np.str_)
array(['', '\x01'], dtype='<U2')
>>> np.array([b'\x00', b'\x01\x00'], np.object_)
array([b'\x00', b'\x01\x00'], dtype=object)
虽然np.object_
有效,但序列化不适合我的用例,因为它序列化指针而不是实际的底层字节数组。
解决方案
推荐阅读
- ruby - 设置关键进程 Ruby FFI
- python - 如何仅使用 boto3 获取与特定名称匹配的 EMR 的 ID
- mysql - 按列追加表 MySQL
- html - 即使名称属性设置正确,jQuery 验证也不起作用
- node.js - Nanoexpress 服务器在 Heroku 和 AWS 上失败 - 在本地运行良好 - 无法跟踪“dest.on()”错误
- python - 全局字典和 dict_items
- python-3.x - python3中如何让每个线程发送多个请求?
- android - 当当前工作人员仍在运行时,Android 工作管理器会在 10 分钟后调用工作人员
- c# - 如何使 Microsoft.Toolkit.Wpf.UI.Controls.WebView 窗口在 xaml 中可调整大小
- c++ - 怎么办我用一些问题来实现 oop