Python 相当于 Java 的 BitSet
是否有Python类或模块实现了类似于BitSet的结构?
Is there a Python class or module that implements a structure that is similar to the BitSet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
标准库里什么都没有。尝试:
http://pypi.python.org/pypi/bitarray
There's nothing in the standard library. Try:
http://pypi.python.org/pypi/bitarray
看看 Python 3 中的实现。
该实现基本上利用了内置的-in
int
类型,这是 Python 3 中的任意精度整数类型(其中long
是 Python 2 的等效类型)。Have a look at this implementation in Python 3.
The implementation basically makes use of the built-in
int
type, which is arbitrary precision integer type in Python 3 (wherelong
is the Python 2 equivalent).我不建议在生产代码中这样做,但为了竞争性编程、面试准备和乐趣,人们应该熟悉位摆弄。
I wouldn't recommend that in production code but for competitive programming, interview preparation and fun, one should make themselves familiar with bit fiddling.
您可能想看一下我编写的名为 bitstring 的模块(完整文档 此处),尽管对于需要尽可能快的简单情况,我仍然推荐 位数组。
一些类似的问题:
什么是在Python中进行位字段操作的最佳方法?
Python有位字段类型吗?
Python 比特流实现
You might like to take a look at a module I wrote called bitstring (full documentation here), although for simple cases that need to be as fast as possible I'd still recommend bitarray.
Some similar questions:
What is the best way to do Bit Field manipulation in Python?
Does Python have a bitfield type?
Python Bitstream implementations
如果位数是有限的,
enum.IntFlag
可以用作位集。请参阅 https://docs.python.org/3/howto/enum.html #intflag
If the number of bits is finite,
enum.IntFlag
can be used as a bit set.See https://docs.python.org/3/howto/enum.html#intflag