numpy boolean array with 1 bit entries(具有 1 位条目的 numpy 布尔数组)
问题描述
在 numpy 中有没有办法创建一个布尔数组,每个条目只使用 1 位?
Is there a way in numpy to create an array of booleans that uses just 1 bit for each entry?
标准的 np.bool 类型是 1 字节,但是这样我使用了 8 倍的所需内存.
The standard np.bool type is 1 byte, but this way I use 8 times the required memory.
在 Google 我发现 C++ 有 std::vector<bool>.
On Google I found that C++ has std::vector<bool>.
推荐答案
你不妨看看 bitstring(文档这里).
You might like to take a look at bitstring (documentation here).
如果你从一个文件创建一个 ConstBitArray 或 ConstBitStream 那么它将使用 mmap 而不会将它加载到内存中.在这种情况下,它不会是可变的,因此如果您想进行更改,则必须将其加载到内存中.
If you create a ConstBitArray or ConstBitStream from a file then it will use mmap and not load it into memory. In this case it won't be mutable so if you want to make changes it will have to be loaded in memory.
例如创建而不加载到内存中:
For example to create without loading into memory:
>>> a = bitstring.ConstBitArray(filename='your_file')
或
>>> b = bitstring.ConstBitStream(a_file_object)
这篇关于具有 1 位条目的 numpy 布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有 1 位条目的 numpy 布尔数组
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
