numpy的布尔数组1位条目 [英] numpy boolean array with 1 bit entries
问题描述
是否有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.
在谷歌,我发现C ++有<一个href=\"http://en.cp$p$pference.com/w/cpp/container/vector_bool\"><$c$c>std::vector<bool>$c$c>
On Google I found that C++ has std::vector<bool>
谢谢!
推荐答案
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)
这篇关于numpy的布尔数组1位条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!