I am trying to use one list to sort another and keep them synchronized at the same time:
keys = [x,x,x,y,y,x,x,z,z,z,x,x]
data = [1,2,3,4,5,6,7,8,9,10,11,12]
I want to use the keys list to organize the data list into subgroups of the same keys.
result = [[1,2,3,6,7,11,12],[4,5,],[8,9,10]]
I also want to make sure that the list is sorted within each subgroup.
so far i was able to get it all sorted properly:
group = []
data = sorted(zip(data, keys), key=lambda x: (x[1]))
for i, grp in groupby(data, lambda x: x[1]):
sub_group = [], []
for j in grp:
sub_group.append(j[1])
group.extend(sub_group)
What else am I missing? Thanks!
dictor anOrderedDictwould be a more useful output, e.g. something like{'x': [1, 2, 3, 6, 7, 11, 12], 'y': [4, 5], 'z': [8, 9, 10]}.