Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extremely large Boolean list in Python

I want to create an object in python that is a collection of around 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, like 123,456,000 is true or false or change its value.

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

I'm a bit noob so you may have to spell things out for me more than if I were asking the question in one of the other languages I know better. Please give me examples of how operating on this object would look.

Thanks

like image 335
Dan Avatar asked Oct 16 '09 19:10

Dan


3 Answers

You can try the bitarray module, or write a similar thing using an array of integers yourself.

like image 108
Lukáš Lalinský Avatar answered Sep 21 '22 18:09

Lukáš Lalinský


"quickly determine if any given number, like 123,456,000 is" in the "true" set or "false" set.

This is what a set is for.

The "true" set is a set of all the numbers.

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

To make a number's boolean flag "false", remove it from the true set.

Life will be much simpler.

like image 21
S.Lott Avatar answered Sep 17 '22 18:09

S.Lott


Have you considered using a lightweight database like SQLite?

like image 30
jldupont Avatar answered Sep 17 '22 18:09

jldupont