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 布尔数组


基础教程推荐
- Kivy 使用 opencv.调整图像大小 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01