I have a question about sort defaultdict in python
Assume I have the following codes:
a = defaultdict(defaultdict)
a['c']['C'] = 1
a['b']['B'] = 2
a['a']['A'] = 3
a['a']['AA'] = 4
I’d like to sort it by the first key and then get another sorted defaultdict like
a['a']['A'] = 3
a['a']['AA'] = 4
a['b']['B'] = 2
a['c']['C'] = 1
I’ve tried using sorted(a.iteritems()) and then get another list. However I need to get dict because I have to iter them.
How can I do this?
This prints the keys and values sorted as you want.