Wednesday, 11 April 2018

Python: sort by value then key

No idea yet how this works, or why.

>>> d = {'apple': 7, 'banana': 3, 'almond': 7, 'peach': 4, 'beetroot': 3}
>>> sorted(d.iteritems(), key=lambda(k, v): (-v, k))
[('almond', 7), ('apple', 7), ('peach', 4), ('banana', 3), ('beetroot', 3)]

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Interview questions: 2020-12

Terraform provider vs provisioner Load balancing Network Load Balancer vs Application Load Balancer  Networking Layer 1 vs Layer 4 haproxy u...