Python Forum
Sorting and working with dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting and working with dictionary
#4
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)]
Reply


Messages In This Thread
Sorting and working with dictionary - by farzankh - Feb-08-2019, 03:27 PM
RE: Sorting and working with dictionary - by snippsat - Feb-08-2019, 04:25 PM

Forum Jump:

User Panel Messages

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