0

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.

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the suggestion! In this particular question, I have implied that values are of some number (float/int). If instead I had a list of lambda functions assigned to the value, would sets work in that particular case? Sorry if this is outside of the scope of the question.
Having the value be of a different type doesn't change the mechanism you'd use to store it and look it up, no. But I'm starting to suspect your actual question is maybe something that's not covered by what you asked. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.