Extremely Large Boolean List in Python

I want to create an object in python that is a collection of 200,000,000 true / false values. So that I can most effectively change or recall any given true / false value so that I can quickly determine if any given number, for example 123,456,000, is true or false, or changes its value.

Is this the best way to make this list? or an array? or class? or just a long int using bitwise operations? or something else?

I am a bit noob, so you may have to write something more for me than if I asked this question in one of the other languages ​​that I know better. Please give me examples of how the work on this object will look.

thanks

+6
python arrays list
source share
5 answers

You can try bitarray or write a similar thing using an array integers.

+12
source share

"quickly determine whether any given number is set, for example 123,456,000, in a" true "set or a" false "set.

This is what set means.

A “true” set is a collection of all numbers.

To make the number a boolean flag "true", add it to the true set.

To make the boolean flag false, remove it from the true set.

Life will be much simpler.

+4
source share

Have you considered using a lightweight database such as SQLite?

+3
source share

You might also like the bitstring module, which is pure Python. Internally, all this is stored as an array of bytes, and masking and bit shifting is done for you:

 from bitstring import BitArray # Initialise with two hundred million zero bits s = BitArray(200000000) # Set a few bits to 1 s.set(1, [76, 33, 123456000]) # And test them if s.all([33, 76, 123456000]): pass 

Other posters are correct, although a simple set may be the best solution for your specific problem.

+3
source share

At first glance, the Python BitVector module sounds as if it does exactly what you want. It is available at http://cobweb.ecn.purdue.edu/~kak/dist/BitVector-1.5.1.html , and since it is pure Python code, it will work on any platform without compilation.

You mentioned that you need a certain speed to get and set any arbitrary true-false value. To do this, you need to use a Python array, not a list, and if you go to the above URL and look at the source code for BitVector, you will see that it really relies on Python arrays.

Ideally, you should encapsulate what you are doing in a class that subclasses from BitVector, i.e.

 class TFValues(BitVector): pass 

That way, you can do things like adding a list to contain related information, such as the name of a specific TF value.

+1
source share

All Articles