Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort a dict in dict
#1
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()))
Reply
#2
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]}]
Reply
#3
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
Reply
#4
(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
Reply
#5
I am trying to understand - is output to screen required(not object bound to name)? If not output then what datatype it is?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unpacking a dict with * or ** msrk 4 919 Dec-02-2023, 11:50 PM
Last Post: msrk
  Why does newly-formed dict only consist of last row of each year? Mark17 6 744 Nov-17-2023, 05:28 PM
Last Post: Mark17
  dict table kucingkembar 4 664 Sep-30-2023, 03:53 PM
Last Post: deanhystad
  replacing dict contents Skaperen 6 1,147 Apr-16-2023, 05:57 PM
Last Post: Skaperen
  search in dict inside tuple steg 1 648 Mar-29-2023, 01:15 PM
Last Post: rob101
  Partial KEY search in dict klatlap 6 1,200 Mar-28-2023, 07:24 AM
Last Post: buran
  Copy item from one dict to another Pavel_47 3 942 Dec-23-2022, 11:19 AM
Last Post: Pavel_47
  'dict()' Has an Exception? htran3 2 723 Nov-04-2022, 08:29 AM
Last Post: htran3
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,208 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,162 Jul-01-2022, 10:52 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020