Reputation: 89
I was wondering what data structure (built-in or not) would be optimal for accessing data that takes 5 conditions as an input.
For example:
if (cond_a == 2) & (cond_b == 3) ... (cond_e == 6):
value = 5
I am looking primarily for speed rather than being memory efficient. There are no relationships between the 5 conditions (they are independent of one another). However, an item can have multiple values for each condition - the data structure would return an iterable of values.
I was considering using nested ordered dictionaries (5 levels deep) - is there a better option?
edit - there may not necessarily be a unique value for all combinations of the 5 conditions. For certain combinations of conditions, changing one condition within that combination may not change the final value.
Upvotes: 0
Views: 37
Reputation: 71454
If you always want the value that matches all five conditions, use a single dictionary with a 5-tuple as the key.
from typing import Dict, Tuple
data: Dict[Tuple[int, int, int, int, int], int] = {
(2, 3, 4, 5, 6): 5
}
value = data[(cond_a, cond_b, cond_c, cond_d, cond_e)]
If you wanted to be able to do queries for values that match only some conditions, then I think you'd want multiple dicts (not nested, with the values stored in sets so you could do intersections).
Upvotes: 1