问题描述
我需要跟踪一组可能在Python中的1000万个数字。 (所有数字在0和2 ^ 32之间)。我会手头知道一个整数的最大值,在0和最大值之间,20-80%的值将在集合中。我的当前代码使用内置的集
。这样太慢了就性能而言,执行此操作的最佳方法是使用bitarray(例如)。
我很容易使用bitarray来构建一个 add(n)
和 remove(n)
方法。我不知道该怎么做是在bitarray_set中支持 n:
。我想我需要使用迭代器或迭代,但我不知道该怎么做。这可能吗?如何?
bitarray
支持 itersearch
方法,迭代在另一个位置发生一个位阵的所有位置。使用它:
def __iter __(self):
return self.bits.itersearch(bitarray([True]) )
I need to track a set of perhaps 10 million numbers in Python. (All numbers are between 0 and 2^32). I'll know before hand the max val of an integer, and, between 0 and max, between 20-80% of the values will be in the set.
My current code uses the built in set
. This way too slow. As far as performance is considered, the best way to do this is with a bitarray (such as https://pypi.python.org/pypi/bitarray/ ).
It's easy for me to use a bitarray to build a class with add(n)
and remove(n)
methods. What I don't know how to do is support for n in bitarray_set:
. I think I need to use an iterator or iterable, but I'm not sure how to do that. Is this possible? How?
bitarray
s support an itersearch
method that iterates over all positions where one bitarray occurs in another. Use that:
def __iter__(self):
return self.bits.itersearch(bitarray([True]))
这篇关于使用位数创建一个像对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!