如何在 Python 中表示和使用 n 位向量?
在我目前正在进行的一项作业中,我们需要使用位向量,但我非常不确定如何在 Python 中执行此操作。它们应该可以是 4 位到 20 位。我以前从未使用过位向量,但我想人们会创建使用通常的 AND/OR/XOR 操作进行操作的无符号字节数组。
这里的重要限制是:除了标准 Python 提供的库之外,我不能依赖任何库。
我想我知道如何在 C 中使用 8 位无符号字节数组来做到这一点: 例如,要将归零数组的第 18 位变为 1,我会执行类似的操作 my_bit_array[3] &= 1<<2
但是由于 Python 是动态类型的并且没有内置数组类型,我将如何以 pythonic 方式执行此操作?
是否有可能(如何?)表达大小为 20 的位向量?我正在考虑制作一个 24 位/3 字节向量并忽略 4 位。
In an assignment I am currently working on we need to work with bit vectors, but I am very unsure of how to do this in Python. They should be able to be from 4 bits to 20 bits. I have never worked with bit vector before, but I guess that one would one create arrays of unsigned bytes that you manipulated using the usual AND/OR/XOR operations.
The important restriction here is: I cannot rely on any libraries other than those supplied with standard Python.
I think I know how I would do this in C using arrays of 8 bit unsigned bytes:
e.g. to turn the 18th bit of a zeroed array into a one, I would do something like
my_bit_array[3] &= 1<<2
But since Python is dynamically typed and does not have a built-in array type, how would I go about doing this in a pythonic way?
And is it possible (how?) to express a bit vector of size 20? I am thinking of perhaps making a 24 bit / 3 byte vector and ignoring the 4 bits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我很惊讶没有人提到
int
(或者我猜是 Python 2 中的long
)。int
可以任意大,您可以对它们使用按位运算符,它们速度很快,并且代码看起来像 C 中的位旋转代码(我认为这是一个优点)。我已将其用于数百位长的位向量。
I'm surprised that no one has mentioned
int
s (or I guesslong
in Python 2).int
s can be arbitrarily large, you can use bitwise operators on them, they're fast, and the code looks like bit twiddling code in C (I consider that to be an advantage).I've used this for bitvectors hundreds of bits long.
库 BitVector 是用于此目的的纯 Python 库,并且应该满足您指定的需求。
The library BitVector is a pure-Python library for this purpose, and should suit the needs you specified.
bitarray 模块可以使用布尔值有效地完成此操作。
The bitarray module does this efficiently with booleans.
它有列表,您可以用布尔值填充:
It has lists, which you can populate with bools:
使用 struct 模块。
Use struct module.
有点过时,但我将在这里留下另一个 stdlib 选项只是为了进行比较。使用 ctypes 模块。
例如:
或者像这样更直接的东西:
A bit dated but I'm going to leave another stdlib option here just for comparison sake. It is also easy to do this using the ctypes module.
For example:
or something more straight forward like this:
还有纯 Python python-bitstring (有 Python 3 支持)。
There is also the pure-Python python-bitstring (with Python 3 support).