python - Sort list of lists that each contain a dictionary -
i have list:
list_users= [[{'points': 9, 'values': 1, 'division': 1, 'user_id': 3}], [{'points': 3, 'values': 0, 'division': 1, 'user_id': 1}], [{'points': 2, 'values': 0, 'division': 1, 'user_id': 4}], [{'points': 9, 'values': 0, 'division': 1, 'user_id': 11}], [{'points': 3, 'values': 0, 'division': 1, 'user_id': 10}], [{'points': 100, 'values': 4, 'division': 1, 'user_id': 2}], [{'points': 77, 'values': 2, 'division': 1, 'user_id': 5}], [{'points': 88, 'values': 3, 'division': 1, 'user_id': 6}], [{'points': 66, 'values': 1, 'division': 1, 'user_id': 7}], [{'points': 2, 'values': 0, 'division': 1, 'user_id': 8}]]
i need sort list points , values.
how can sort if dict inside list inside main list?
i generated list query , append list_users
?
sort using key
function sorted
builds tuple of points
, values
each dict in each list.
def kf(x): return (x[0]["points"], x[0]["values"]) s = sorted(list_users, key=kf) print(s)
output:
[[{'division': 1, 'points': 2, 'user_id': 4, 'values': 0}], [{'division': 1, 'points': 2, 'user_id': 8, 'values': 0}], [{'division': 1, 'points': 3, 'user_id': 1, 'values': 0}], [{'division': 1, 'points': 3, 'user_id': 10, 'values': 0}], [{'division': 1, 'points': 9, 'user_id': 11, 'values': 0}], [{'division': 1, 'points': 9, 'user_id': 3, 'values': 1}], [{'division': 1, 'points': 66, 'user_id': 7, 'values': 1}], [{'division': 1, 'points': 77, 'user_id': 5, 'values': 2}], [{'division': 1, 'points': 88, 'user_id': 6, 'values': 3}], [{'division': 1, 'points': 100, 'user_id': 2, 'values': 4}]]
Comments
Post a Comment