Python 有位域类型吗? [英] Does Python have a bitfield type?

查看:69
本文介绍了Python 有位域类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个布尔数组的紧凑表示,Python 是否具有内置位域类型,或者我是否需要找到替代解决方案?

解决方案

Bitarray 是我找到的最佳答案,当我最近有类似的需求时.它是一个 C 扩展(比纯 Python 的 BitVector 快得多)并将其数据存储在实际的位域中(因此它的内存效率是 numpy 布尔数组的八倍,后者似乎每个元素使用一个字节.)

I need a compact representation of an array of booleans, does Python have a builtin bitfield type or will I need to find an alternate solution?

解决方案

Bitarray was the best answer I found, when I recently had a similar need. It's a C extension (so much faster than BitVector, which is pure python) and stores its data in an actual bitfield (so it's eight times more memory efficient than a numpy boolean array, which appears to use a byte per element.)

这篇关于Python 有位域类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆