Using an array of strings as keys to dicts within dicts
I'm a firmware guy, so I'll be the first to admit that my python solution can often be less the elegant.
Anyway as a personal project, I'm parsing a file following the OSC protocol, while has parameters in a similar format to Linux directories (/path/to/paramater) followed by that values for the parameter. In parsing it, I can easily separate the parameter path from the value, but I'm wanting to create dicts within dicts that eventually hold the parameter values. My problem is that the path may have a varying number of "subfolders." Is there a clean way to use an array of the format ["path", "to", "value"] to create and access a dict as d["path"]["to"]["value"] where the number of embedded dicts is variable?
Posted Jun 25, 2016, 8:32 AM UTC
the easiest way to create multidimensional arrays in python is to use tuples as keys.
I think I'll try u/K900_'s suggestion. Yours is definitely reasonable, but has some issue with the application. It's for an audio console and I'd like to be able to easily access channels in order to do things like:
Copy /ch/01/... to /ch/02/...
Or
Insert something on /ch/12/... and bump the former /ch/12/.. to /ch/13/... and all additional channels over one.
Edit: Actually, on further analysis this may be the way to go.