Group data in nested dictionary Python -
i have dictionary this,
data = {'04-01-2012': [{1: 0.93}, {2: 0.9195000000000001}, {3: 0.9090000000000001}, {4: 0.8985000000000002}, {5: 0.8880000000000002}, {6: 0.8775000000000003}, {7: 0.8670000000000003}, {8: 0.8565000000000004}, {9: 0.8460000000000004}], '12-01-2012': [{1: 0.96}], '07-01-2012': [{1: 0.96}, {2: 0.95}, {3: 0.94}, {4: 0.9299999999999999}, {5: 0.9199999999999999}, {6: 0.9099999999999999}], '06-01-2012': [{1: 0.945}, {2: 0.9365}, {3: 0.928}, {4: 0.9195000000000001}, {5: 0.9110000000000001}, {6: 0.9025000000000002}, {7: 0.8940000000000002}], '10-01-2012': [{1: 0.93}, {2: 0.9244}, {3: 0.9188}], '05-01-2012': [{1: 0.935}, {2: 0.926}, {3: 0.917}, {4: 0.908}, {5: 0.899}, {6: 0.89}, {7: 0.881}, {8: 0.872}], '11-01-2012': [{1: 0.945}, {2: 0.9325}], '02-01-2012': [{1: 0.94}, {2: 0.9299999999999999}, {3: 0.9199999999999999}, {4: 0.9099999999999999}, {5: 0.8999999999999999}, {6: 0.8899999999999999}, {7: 0.8799999999999999}, {8: 0.8699999999999999}, {9: 0.8599999999999999}, {10: 0.8499999999999999}, {11: 0.8399999999999999}], '03-01-2012': [{1: 0.955}, {2: 0.9455}, {3: 0.936}, {4: 0.9265000000000001}, {5: 0.9170000000000001}, {6: 0.9075000000000002}, {7: 0.8980000000000002}, {8: 0.8885000000000003}, {9: 0.8790000000000003}, {10: 0.8695000000000004}], '08-01-2012': [{1: 0.94}, {2: 0.9295}, {3: 0.919}, {4: 0.9085000000000001}, {5: 0.8980000000000001}], '01-01-2012': [{1: 0.95}, {2: 0.94}, {3: 0.9299999999999999}, {4: 0.9199999999999999}, {5: 0.9099999999999999}, {6: 0.8999999999999999}, {7: 0.8899999999999999}, {8: 0.8799999999999999}, {9: 0.8699999999999999}, {10: 0.8599999999999999}, {11: 0.8499999999999999}, {12: 0.8399999999999999}], '09-01-2012': [{1: 0.92}, {2: 0.91}, {3: 0.9}, {4: 0.89}]}
i need iterate on dictionary values , group 1's, 2's , on.
this code far
from collections import defaultdict final = defaultdict(list) k, v in data.items(): new_data = next(iter(v)) m, n in new_data.items(): final[m].append(n) print(final) # defaultdict(<class 'list'>, {1: [0.935, 0.92, 0.955, 0.96, 0.94, 0.93, 0.95, 0.96, 0.945, 0.94, 0.945, 0.93]})
it groups 1's only, not 2's , on. wrong doing?
you forgot iterate on many tiny dictionaries:
from collections import defaultdict final = defaultdict(list) k, v in data.items(): d in v: # <-- missing m, n in d.items(): final[m].append(n) print(final)
(you called next(...)
, yields first item only.)
output:
defaultdict(, {1: [0.96, 0.935, 0.93, 0.945, 0.96, 0.95, 0.93, 0.94, 0.945, 0.955, 0.94, 0.92], 2: [0.926, 0.9244, 0.9365, 0.95, 0.94, 0.9195000000000001, 0.9299999999999999, 0.9325, 0.9455, 0.9295, 0.91], 3: [0.917, 0.9188, 0.928, 0.94, 0.9299999999999999, 0.9090000000000001, 0.9199999999999999, 0.936, 0.919, 0.9], 4: [0.908, 0.9195000000000001, 0.9299999999999999, 0.9199999999999999, 0.8985000000000002, 0.9099999999999999, 0.9265000000000001, 0.9085000000000001, 0.89], 5: [0.899, 0.9110000000000001, 0.9199999999999999, 0.9099999999999999, 0.8880000000000002, 0.8999999999999999, 0.9170000000000001, 0.8980000000000001], 6: [0.89, 0.9025000000000002, 0.9099999999999999, 0.8999999999999999, 0.8775000000000003, 0.8899999999999999, 0.9075000000000002], 7: [0.881, 0.8940000000000002, 0.8899999999999999, 0.8670000000000003, 0.8799999999999999, 0.8980000000000002], 8: [0.872, 0.8799999999999999, 0.8565000000000004, 0.8699999999999999, 0.8885000000000003], 9: [0.8699999999999999, 0.8460000000000004, 0.8599999999999999, 0.8790000000000003], 10: [0.8599999999999999, 0.8499999999999999, 0.8695000000000004], 11: [0.8499999999999999, 0.8399999999999999], 12: [0.8399999999999999]})
Comments
Post a Comment