sorting - how to print a dictionary sorted by a value of a subdictionary? -
i have dictionary within dictionary , wish print whole dictionary sorted around value in sub dictionary
lesson = {name:{'rating':rating, 'desc':desc, 'timelefttask':timeleft}} or
lesson = {'math':{'rating':11, 'desc':'exercises 14 , 19 page 157', 'timelefttask':7}, 'english':{'rating':23, 'desc':'exercise 5 page 204', 'timelefttask':2}} i want print dict illustration sorted 'rating' (high numbers @ top) have read this post don't understand it. if maintain simple great. , yes i'm making programme sort , deal homework in advance
def sort_by_subdict(dictionary, subdict_key): homecoming sorted(dictionary.items(), key=lambda k_v: k_v[1][subdict_key]) lesson = {'math':{'rating':11, 'desc':'exercises 14 , 19 page 157', 'timelefttask':7}, 'english':{'rating':23, 'desc':'exercise 5 page 204', 'timelefttask':2}} print(sort_by_subdict(lesson, 'rating'))
as there no notion of order in dictionary, need represent dictionary list of key, value pair tuples preserve sorted order.
the so question mention sorts dictionary using sorted function such returns list of (k, v) tuples (here k means key & v means value) of top level dictionary, sorting desired value of sub dictionary v.
sorting python-3.x dictionary printing
No comments:
Post a Comment