Python Forum
Easy way to sort a nested dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Easy way to sort a nested dict
#3
Buran's idea is very good. Here is a variation with some abstraction
#!/usr/bin/python3
from collections import OrderedDict
 
db = \
{
  "/data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-17 Disc Wars.mp3": {
    "duration": "00:04:11",
    "artist": "Daft Punk",
    "album": "Tron: Legacy (Cd1)",
    "track": "17",
    "title": "Disc Wars"
  },
  "/data/Music/Keith Kouna/Les ann\u00e9es monsieur/07 - Godichons.mp3": {
    "duration": "00:05:09",
    "artist": "Keith Kouna",
    "album": "Les ann\u00e9es monsieur",
    "track": "7",
    "title": "Godichons"
  },
  "/data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-07 Rinzler.mp3": {
    "duration": "00:02:17",
    "artist": "Daft Punk",
    "album": "Tron: Legacy (Cd1)",
    "track": "07",
    "title": "Rinzler"
  },
  "/data/Music/Keith Kouna/Les ann\u00e9es monsieur/10 - Le Tape.mp3": {
    "duration": "00:06:06",
    "artist": "Keith Kouna",
    "album": "Les ann\u00e9es monsieur",
    "track": "10",
    "title": "Le Tape"
  },
  "/data/Music/Keith Kouna/Les ann\u00e9es monsieur/09 - L'or.mp3": {
    "duration": "00:05:05",
    "artist": "Keith Kouna",
    "album": "Les ann\u00e9es monsieur",
    "track": "9",
    "title": "L'or"
  }
}
  
from functools import wraps

def dbsorter(func):
    @wraps(func)
    def wrapper(db, reverse=False):
        return OrderedDict(sorted(
            db.items(), reverse=bool(reverse), key=lambda item: func(*item)))
    return wrapper

@dbsorter
def by_artist_and_track(key, data):
    return data['artist'], int(data['track'])

sdb = by_artist_and_track(db)
for key in sdb:
    print(key)
print()

@dbsorter
def by_duration(key, data):
    h, m, s = (int(x) for x in data['duration'].split(':'))
    return (h, m, s)

sdb = by_duration(db, reverse=True)
for key in sdb:
    print(key)
Output:
/data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-07 Rinzler.mp3 /data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-17 Disc Wars.mp3 /data/Music/Keith Kouna/Les années monsieur/07 - Godichons.mp3 /data/Music/Keith Kouna/Les années monsieur/09 - L'or.mp3 /data/Music/Keith Kouna/Les années monsieur/10 - Le Tape.mp3 /data/Music/Keith Kouna/Les années monsieur/10 - Le Tape.mp3 /data/Music/Keith Kouna/Les années monsieur/07 - Godichons.mp3 /data/Music/Keith Kouna/Les années monsieur/09 - L'or.mp3 /data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-17 Disc Wars.mp3 /data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-07 Rinzler.mp3
Reply


Messages In This Thread
Easy way to sort a nested dict - by Alfalfa - Dec-07-2018, 04:21 AM
RE: Easy way to sort a nested dict - by buran - Dec-07-2018, 08:34 AM
RE: Easy way to sort a nested dict - by Gribouillis - Dec-07-2018, 10:07 AM
RE: Easy way to sort a nested dict - by Alfalfa - Dec-07-2018, 04:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,358 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Updating nested dict list keys tbaror 2 1,313 Feb-09-2022, 09:37 AM
Last Post: tbaror
  changing key names in nested dict wardancer84 6 2,199 Sep-10-2021, 08:13 AM
Last Post: wardancer84
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,935 Jan-22-2021, 05:43 AM
Last Post: buran
  [nested dics] Easy way to find if a key exists? Winfried 9 3,039 Sep-17-2020, 05:30 PM
Last Post: deanhystad
  Sort a dict in dict cherry_cherry 4 82,340 Apr-08-2020, 12:25 PM
Last Post: perfringo
  convert List of Dicts into a 2 deep Nested Dict rethink 1 3,253 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  How can I sort my dict ? Mike Ru 1 1,844 Feb-16-2019, 11:50 PM
Last Post: ichabod801
  how to create a nested dict.. wardancer84 5 3,733 Nov-23-2018, 04:01 AM
Last Post: Larz60+
  sorting nested dict according to values merlem 6 17,704 Apr-01-2017, 10:01 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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