Given a dictionary loaded from the following YAML:
song1:
chunk1:
attr1: value
attr2: value
attr3: value
chunk2:
attr1: value
attr2: value
attr3: value
chunk3:
attr1: value
attr2: value
attr3: value
song2:
chunk1:
attr1: value
... and so on
How can one go about reordering the keys to get something like this:
attr1:
song1:
chunk1: value
chunk2: value
chunk3: value
song2:
chunk1: value
chunk2: value
chunk3: value
song3:
chunk1: value
chunk2: value
chunk3: value
attr2:
... and so on
I think I know how to do this but I wanted to look for any optimized methods to solve this, preferably in Python using YAML but general solutions also welcome. Additionally, what is the name of this type of problem?
Edit: a rudimentary solution with python dictionaries, but I was wondering if there's any other way apart from a brute force approach to this:
reordered = {}
for song_name in output.keys():
for chunk_name in output[song_name].keys():
for attr_name in output[song_name][chunk_name].keys():
if attr_name not in reordered:
reordered[attr_name] = {}
if song_name not in reordered[attr_name]:
reordered[attr_name][song_name] = {}
reordered[attr_name][song_name][chunk_name] = output[song_name][chunk_name][attr_name]