Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting and working with dictionary
Can somebody help with this question?

Given the dictionary, medals, sort it by the medal count. Save the three countries with the highest medal count to the list, top_three.

medals = {'Japan':41, 'Russia':56, 'South Korea':21, 'United States':121, 'Germany':42, 'China':70}
This question may seem very easy to many of you, but i'm curious how you approach it.

first, a dictionary is a hashed table, so by default is not sort-able other than by insertion ordered (which is automatic with versions of python starting with 3.7.
you can, however extract the keys into a list, and then sort the list, effectively creating an ordered index into the dictionary
Example with your dictionary:
>>> medals = {'Japan':41, 'Russia':56, 'South Korea':21, 'United States':121, 'Germany':42, 'China':70}
>>> keys = medals.keys()
>>> for key in keys:
...     print(f'key: {key}, value: {medals[key]}') 
key: Japan, value: 41
key: Russia, value: 56
key: South Korea, value: 21
key: United States, value: 121
key: Germany, value: 42
key: China, value: 70
>>> # or by medal count:
>>> klist = []
>>> for key, value in medals.items():
...     klist.append([key, value])
>>> klist
[['Japan', 41], ['Russia', 56], ['South Korea', 21], ['United States', 121], ['Germany', 42], ['China', 70]]
>>> sorted(klist, key=lambda klist: klist[1])
[['South Korea', 21], ['Japan', 41], ['Germany', 42], ['Russia', 56], ['China', 70], ['United States', 121]]

Iterate over medals.items(), which will give you key/value pairs. Make a list of value/key tuples. Sorting that list will sort by value.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Just a advice,you are doing a unnecessary step with klist Larz60+.
>>> medals = {'Japan':41, 'Russia':56, 'South Korea':21, 'United States':121, 'Germany':42, 'China':70}
>>> sorted(medals.items(), key=lambda v: v[1])
[('South Korea', 21),
 ('Japan', 41),
 ('Germany', 42),
 ('Russia', 56),
 ('China', 70),
 ('United States', 121)]
(Feb-08-2019, 03:27 PM)farzankh Wrote: Save the three countries with the highest medal count to the list, top_three.
>>> medals = {'Japan':41, 'Russia':56, 'South Korea':21, 'United States':121, 'Germany':42, 'China':70}
>>> sorted(medals.items(), key=lambda v: v[1], reverse=True)[:3]
[('United States', 121), ('China', 70), ('Russia', 56)]

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  sorting row values alphabetical PolskaYBZ 1 80 Jan-27-2019, 01:49 PM
Last Post: stullis
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 195 Dec-19-2018, 06:22 PM
Last Post: ichabod801
  Class sort not sorting corretly chrissowden14 7 211 Dec-09-2018, 11:53 PM
Last Post: chrissowden14
  sorting a deck of cards (objects in a list) itmustbebunnies 1 277 Dec-05-2018, 02:44 AM
Last Post: ichabod801
  Help with list sorting gonzo620 1 252 Oct-16-2018, 02:58 PM
Last Post: j.crater
  Sorting list of names by first two characters Otbredbaron 2 471 May-24-2018, 03:59 PM
Last Post: Otbredbaron
  Playing Card Sorting ness828 4 505 Feb-05-2018, 09:01 PM
Last Post: sparkz_alot
  Bubble Sort Algorithm Not Sorting LikedList Contents JayJayOi 0 1,006 Jan-19-2018, 12:09 AM
Last Post: JayJayOi
  Advanced sorting of a built-in list Whisper40 6 785 Jan-11-2018, 07:27 PM
Last Post: Whisper40
  I Can't Get This Sorting Function In This LinkedList Code To Work JayJayOi 10 2,129 Jan-11-2018, 01:14 AM
Last Post: JayJayOi

Forum Jump:

Users browsing this thread: 1 Guest(s)