python - I have five variables that will have different values based on whether they are in Alaska/US and at half/full resolution -


question appropriate data structure:

i have 5 variables have different values string based on whether in us/alaska , @ half/full resolution.

so i'm building 5/2/2 (array or list or dict).

i want access x = dstr(var,'ak','h') , e.g.. alaska/half-res, values osp/ul/lr/etc, variables?

this static table, values won't change. there no obvious ordering demand 0,1,2 indices

problem is, array doesn't string indices , dict wants 1 key only, not three.

any ideas?

you can use tuples index dict:

>>> values = { ...     ("ak", "h"): ("some", "sample", "data", "to", "return"), ...     ("ak", "f"): ("more", "sample", "data", "for", "you"), ...     # more data here ... } >>> a, b, c, d, e = values[("ak", "h")] >>> "some" 

or can use nest of dicts:

>>> values = { ...     "ak": { ...         "h": ("some", "sample", "data", "to", "return"), ...         "f": ("more", "sample", "data", "for", "you") ...     }, ...     # other nested dicts here ... } >>> a, b, c, d, e = values["ak"]["h"] >>> "some" 

if have class structure defining 5 data points part of single object (which idea keep data grouped if related), can store instances of class instead of tuples:

>>> values = { ...     ("ak", "h"): myclass("some", "sample", "data", "to", "return"), ...     ("ak", "f"): myclass("more", "sample", "data", "for", "you"), ...     # more data here ... } >>> obj = values[("ak", "h")] >>> obj.osp "some" 

or

>>> values = { ...     "ak": { ...         "h": myclass("some", "sample", "data", "to", "return"), ...         "f": myclass("more", "sample", "data", "for", "you") ...     }, ...     # other nested dicts here ... } >>> obj = values["ak"]["h"] >>> obj.osp "some" 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -