Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Greedy Cow Transport
#11
(Apr-10-2019, 09:33 PM)Truman Wrote: but it makes a tuple of it which is not good. It seems that dictionaries can't be sorted by value.

Up to Python 3.6 sorting dictionaries has no point as dictionaries were unordered. Starting from 3.6 they became insertion ordered and from 3.7 dictionaries are guaranteed to be insertion ordered. If relying on dictionaries order one must be sure that version >3.6 is used.

Tuples are good. You just wrap into dict() and there you are:

>>> d = {'Maggie': 3, 'Herman': 7, 'Betsy': 9, 'Oreo': 6, 'Moo Moo': 3, 'Milkshake': 2, 
...      'Millie': 5, 'Lola': 2, 'Florence': 2, 'Henrietta': 9}
>>> n = dict(sorted(d.items(), key=lambda x: x[1])) 
>>> n
{'Milkshake': 2, 'Lola': 2, 'Florence': 2, 'Maggie': 3, 'Moo Moo': 3, 'Millie': 5, 'Oreo': 6, 'Herman': 7, 'Betsy': 9, 'Henrietta': 9}
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


Messages In This Thread
Greedy Cow Transport - by Truman - Apr-08-2019, 10:13 PM
RE: Greedy Cow Transport - by Gribouillis - Apr-08-2019, 10:22 PM
RE: Greedy Cow Transport - by ichabod801 - Apr-08-2019, 10:30 PM
RE: Greedy Cow Transport - by perfringo - Apr-09-2019, 05:19 AM
RE: Greedy Cow Transport - by Skaperen - Apr-09-2019, 05:49 AM
RE: Greedy Cow Transport - by Truman - Apr-09-2019, 10:29 PM
RE: Greedy Cow Transport - by ichabod801 - Apr-09-2019, 10:45 PM
RE: Greedy Cow Transport - by Truman - Apr-09-2019, 10:57 PM
RE: Greedy Cow Transport - by perfringo - Apr-10-2019, 03:28 AM
RE: Greedy Cow Transport - by Truman - Apr-10-2019, 09:33 PM
RE: Greedy Cow Transport - by perfringo - Apr-11-2019, 04:03 AM
RE: Greedy Cow Transport - by perfringo - Apr-11-2019, 11:38 AM
RE: Greedy Cow Transport - by Truman - Apr-11-2019, 11:06 PM
RE: Greedy Cow Transport - by perfringo - Apr-12-2019, 04:40 AM
RE: Greedy Cow Transport - by Truman - Apr-14-2019, 11:18 PM
RE: Greedy Cow Transport - by perfringo - Apr-15-2019, 07:14 AM
RE: Greedy Cow Transport - by Truman - Apr-15-2019, 08:42 PM
RE: Greedy Cow Transport - by perfringo - Apr-18-2019, 10:47 AM
RE: Greedy Cow Transport - by Truman - Apr-18-2019, 02:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I correct multiplication error? Greedy algorithm help student8 1 3,927 Oct-02-2017, 03:00 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

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