I have problem about sorting my dict. My code is:
x = {('S', 'A'): (5, 8), ('S', 'B'): (11, 17), ('S', 'C'): (8, 14)}
sort_x = sorted(x.items(), key=lambda kv: kv[1])
print sort_x
sort_x_dict = dict(sort_x)
print sort_x_dict
Output:
[(('S', 'A'): (5, 8)), (('S', 'C'): (8, 14)), (('S', 'B'): (11, 17))]
{('S', 'A'): (5, 8), ('S', 'B'): (11, 17), ('S', 'C'): (8, 14)}
dictaren't ordered, so sorting them makes no sense. Maybe you want to useOrederedDict?dicts do retain order sodict(sort_x)loses the order ofsort_x.collections.OrderedDictwill keep order.sort_x:{('S', 'A'): (5, 8), ('S', 'C'): (8, 14), ('S', 'B'): (11, 17)}.