Apr-24-2019, 03:00 AM
I don't clearly understand the question, but I wrote some text that could help you to find
solution for your case, I think.
Ok, we have a database presented by a list of tuples (key, value); That database could include multiple keys, e.g. data = [(key1, val1), (key1, val2), (key1, val3), (key2, val7)....]
Lets a user have selected some checkboxes, e.g.
binary value
any particular checkbox selection is already presented in binary form.
Now we need to make a query to our database. There are two general form of queries could be done here:
1) find records, where at least one checkbox is the same, as in the database;
2) find records, where all selected (unselected) checkboxes are the that, as in the database;
How to compare two keys (checkbox selection and record key)?
To check full equality, it is sufficient to use comparison operator
0b0000001010 == 0b0000000101 => False
0b0000001010 == 0b0000001010 => True # all checkboxes should be the same, as in the db
If we need to perform comparison by "at least one checkbox equal" scheme, we can
use bitwise
complexity estimation (you need to check all elements in the db). This might be inappropriate in case of millions records or more.
So, all combinations of 10 items could be masked as binary numbers from 0 to 1023. To traverse
all possible combinations (masks) in this case you just need iterate over range(0, 1024) and apply
function. Hope this helps.
solution for your case, I think.
Ok, we have a database presented by a list of tuples (key, value); That database could include multiple keys, e.g. data = [(key1, val1), (key1, val2), (key1, val3), (key2, val7)....]
data = [(x, 'message #{}'.format(x)) for x in range(2 ** 10)] # [(key, value), ...] #kv-store, simple modelYou can choose another model of the db, or use external db solution. This is a simple model of a database.
Lets a user have selected some checkboxes, e.g.
(0,0,0,0,0,0,1,0,1,0)
, this could be converted to somebinary value
0b0000001010
that coincedes to 10 (in decimal number system). So, we could treat that any particular checkbox selection is already presented in binary form.
Now we need to make a query to our database. There are two general form of queries could be done here:
1) find records, where at least one checkbox is the same, as in the database;
2) find records, where all selected (unselected) checkboxes are the that, as in the database;
How to compare two keys (checkbox selection and record key)?
To check full equality, it is sufficient to use comparison operator
==
.0b0000001010 == 0b0000000101 => False
0b0000001010 == 0b0000001010 => True # all checkboxes should be the same, as in the db
If we need to perform comparison by "at least one checkbox equal" scheme, we can
use bitwise
and
operator. bool(0b0000001010 & 0b0000000010)
Output:True
bool(0b0000001010 & 0b1100000000)
Output:False
Let we have:user_selected_checkboxes = 0b0000000100
at_least_one_criteria_met = [v for k, v in data if k & user_selected_checkboxes] len(at_least_one_criteria_met)
Output:512
all_criteria_met = [v for k, v in data if k == user_selected_checkboxes] all_criteria_met
Output:['message #1']
You still need to adopt this for your needs. Note: searching in this cases has O(N) ascomplexity estimation (you need to check all elements in the db). This might be inappropriate in case of millions records or more.
So, all combinations of 10 items could be masked as binary numbers from 0 to 1023. To traverse
all possible combinations (masks) in this case you just need iterate over range(0, 1024) and apply
bin
function. Hope this helps.