I think using OrderedDict is the best way. They are built-in and relatively fast:
custom = OrderedDict([(1, OrderedDict([('a', np.zeros(10)), ('b', np.zeros(100))])), (2, OrderedDict([('c', np.zeros(20)), ('d', np.zeros(200))]))])
If you want to simplify the iteration of the contents of your data structure, you can always provide a utility function for this:
def iter_over_contents(data_structure): for delem in data_structure.values(): for v in delem.values(): for row in v: yield row
Note that in Python 3.3+, which allows yield from <expression> , you can exclude the last for loop:
def iter_over_contents(data_structure): for delem in data_structure.values(): for v in delem.values(): yield from v
With one of them, you can write something like:
for elem in iter_over_contents(custom): print(elem)
and hide the difficulty.
While you could define your own class in an attempt to encapsulate this data structure and use something like the iter_over_contents() generator function as the __iter__() method, this approach will probably be slower and won't allow expressions using two indexing levels such as:
custom[1]['b']
which uses nested dictionaries (or OrderedDefaultdict , as shown in my other answer).
source share