Python 位数组集 [英] Python bitarray set

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

问题描述

生成一组类似位数组的对象的最佳方法是什么,以便我可以有效地测试成员资格.天真的方式似乎并不像我期望的那样工作:

<预><代码>>>>从位阵列导入位阵列>>>>>>bitarray_set = set([bitarray('0000'), bitarray('0001')])>>>bitarray_set设置([位数组('0001'),位数组('0000')])>>>>>>bitarray_set 中的 bitarray('0000')错误的

一种解决方法是保留一组单独的字符串或其他更友好的对象作为键.然后将一个位数组转换为一个字符串,并根据这个集合测试成员资格.但这似乎有点麻烦.有没有更好的解决方案?

解决方案

看来 bitarray 不维护哈希不变量:

<预><代码>>>>x = 位数组(b'0000')>>>y = 位数组(b'0000')>>>x == y真的>>>散列(x) == 散列(y)错误的

这违反了 __hash__ 的 API,因为 记录:

<块引用>

唯一需要的属性是比较相等的对象具有相同的哈希值

这意味着位数组实际上是不可散列的,并且不能在集合中或作为字典键可靠地工作.

我认为这是位数组库中的一个错误.我以前从未听说过 bitarray,它似乎没有太多文档.据我所知,它甚至没有说明应该如何为位数组定义相等性,也没有说明它们是否应该是可散列的,但它似乎以不兼容的方式实现了相等性和散列.

What is the best way to generate a set of bitarray-like objects so that I can test for membership efficiently. The naive way doesn't seem to work as I expect:

>>> from bitarray import bitarray
>>> 
>>> bitarray_set = set([bitarray('0000'), bitarray('0001')])
>>> bitarray_set
set([bitarray('0001'), bitarray('0000')])
>>> 
>>> bitarray('0000') in bitarray_set
False

A workaround is to keep a separate set of strings or other more friendly object as keys. Then convert a bitarray to a string and test membership against this set instead. But that seems a bit cumbersome. Is there a better solution?

解决方案

It appears that bitarray does not maintain the hash invariant:

>>> x = bitarray(b'0000')
>>> y = bitarray(b'0000')
>>> x == y
True
>>> hash(x) == hash(y)
False

This is a violation of the API for __hash__, as documented:

The only required property is that objects which compare equal have the same hash value

This mean that bitarrays are effectively unhashable and will not work reliably in sets or as dictionary keys.

I would regard this as a bug in the bitarray library. I had never heard of bitarray before, and it doesn't seem to have much documentation. As far as I can see it doesn't even say how equality is supposed to be defined for bitarrays, nor whether they are supposed to be hashable, but it seems that it implements equality and hashing in incompatible ways.

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

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