Apr-11-2019, 04:03 AM
(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.
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.