Python Forum
Sort a dict in dict - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Sort a dict in dict (/thread-25679.html)



Sort a dict in dict - cherry_cherry - Apr-07-2020

Hi everyone,
I want to sort by 'name' value, by 'notes' values and by average 'notes' values in a dictionary. I tried different methods but it didn't work. I have searched for a solution on google but there's no solution that works. I hope you will help me.

This's my code I'm trying a sort by 'name'.

dico_nouveau = {'20202020': {'name': 'Durand', 'first': 'Martin', 'notes': [15, 15.5, 8, 13]}, '21212121': {'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]}, '28790020': {'name': 'Férien', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}, '20212021': {'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}, '21202120': {'name': 'Allard', 'first': 'Chloé', 'notes': [11, 9.5, 2, 17]}, '29019022': {'name': 'Durand', 'first': 'Alan', 'notes': [12, 15.5, 8, 13]}}


for e in sorted(dico_nouveau.items(), key = lambda x : (x[1].values())):  print(e,"-", tuple(dico_nouveau[e].values()))



RE: Sort a dict in dict - bowlofred - Apr-07-2020

I'm not sure I understand what you're trying to do. Do you want 3 different sorts, or one sort that has preferential keys? And what information do you want to return. Do you need the initial keys or just the value objects?

This returns the keys and the names of dict in 'name' order
>>> [(k, v['name']) for k,v in sorted(dico_nouveau.items(), key=lambda x: x[1]['name'])]
[('21212121', 'Dupond'), ('20202020', 'Durand'), ('28790020', 'Férien')]
or to just return all the dictionary objects in 'name' order:
>>> sorted(dico_nouveau.values(), key=lambda v: v['name'])
[{'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]}, {'name': 'Durand', 'first': 'Martin', 'notes': [15, 15.5, 8, 13]}, {'name': 'Férien', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}]



RE: Sort a dict in dict - deanhystad - Apr-07-2020

Stick with functions until you have things working, then you can try converting to a lambda. For something like this I see no reason to use lambdas at all.
import statistics

dico_nouveau = {
    '20202020': {'name': 'Durand', 'first': 'Martin', 'notes': [15, 15.5, 8, 13]},
    '21212121': {'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]},
    '28790020': {'name': 'Férien', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]},
    '20212021': {'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]},
    '21202120': {'name': 'Allard', 'first': 'Chloé', 'notes': [11, 9.5, 2, 17]},
    '29019022': {'name': 'Durand', 'first': 'Alan', 'notes': [12, 15.5, 8, 13]}}
 
def name(d):
    return d['name'].lower() + d['first'].lower()

def notes(d):
    return statistics.mean(d['notes'])

print('Sort by name')
for e in sorted(dico_nouveau.values(), key=name):
    print(e['name'], e['first'], sep=', ')

print('\nSort by average notes')
for e in sorted(dico_nouveau.values(), key=notes):
    print(e['name'], e['first'], sep=', ')

print('\nBy first name using lambda')
for e in sorted(dico_nouveau.values(),
                key=lambda d: d['first'].lower() + d['name'].lower()):
    print(e['first'], e['name'])
Output:
Sort by name Allard, Chloé Bosse, Mélissa Dupond, Alain Durand, Alan Durand, Martin Férien, Mélissa Sort by average notes Allard, Chloé Dupond, Alain Durand, Alan Durand, Martin Férien, Mélissa Bosse, Mélissa By first name using lambda Alain Dupond Alan Durand Chloé Allard Martin Durand Mélissa Bosse Mélissa Férien



RE: Sort a dict in dict - cherry_cherry - Apr-08-2020

(Apr-07-2020, 09:57 PM)bowlofred Wrote: sorted(dico_nouveau.values(), key=lambda v: v['name'])

Thank you for reply!
I want 3 different sorts and the information output include the initial key and a sort like this:

Output:
202020 - 'Dupond','Alain', [11, 9.5, 5.5, 18]
and with a sort by average 'notes' values I also want to return the average value.
Output:
202020 - 'Dupond','Alain', [11, 9.5, 5.5, 18] - 14.8

(Apr-07-2020, 10:07 PM)deanhystad Wrote: Stick with functions until you have things working, then you can try converting to a lambda. For something like this I see no reason to use lambdas at all.
import statistics

dico_nouveau = {
    '20202020': {'name': 'Durand', 'first': 'Martin', 'notes': [15, 15.5, 8, 13]},
    '21212121': {'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]},
    '28790020': {'name': 'Férien', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]},
    '20212021': {'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]},
    '21202120': {'name': 'Allard', 'first': 'Chloé', 'notes': [11, 9.5, 2, 17]},
    '29019022': {'name': 'Durand', 'first': 'Alan', 'notes': [12, 15.5, 8, 13]}}
 
def name(d):
    return d['name'].lower() + d['first'].lower()

def notes(d):
    return statistics.mean(d['notes'])

print('Sort by name')
for e in sorted(dico_nouveau.values(), key=name):
    print(e['name'], e['first'], sep=', ')

print('\nSort by average notes')
for e in sorted(dico_nouveau.values(), key=notes):
    print(e['name'], e['first'], sep=', ')

print('\nBy first name using lambda')
for e in sorted(dico_nouveau.values(),
                key=lambda d: d['first'].lower() + d['name'].lower()):
    print(e['first'], e['name'])
Output:
Sort by name Allard, Chloé Bosse, Mélissa Dupond, Alain Durand, Alan Durand, Martin Férien, Mélissa Sort by average notes Allard, Chloé Dupond, Alain Durand, Alan Durand, Martin Férien, Mélissa Bosse, Mélissa By first name using lambda Alain Dupond Alan Durand Chloé Allard Martin Durand Mélissa Bosse Mélissa Férien

Thank you so much!
If I want the information to return include the key, how can I write?

Output like this:
Output:
212121 - Dupond', 'Alain', [11, 9.5, 5.5, 18] 202020 - 'Durand', 'Martin', [15, 15.5, 8, 13] with average: 202020 - 'Dupond','Alain', [11, 9.5, 5.5, 18] - 14.8



RE: Sort a dict in dict - perfringo - Apr-08-2020

I am trying to understand - is output to screen required(not object bound to name)? If not output then what datatype it is?