{"id":91,"date":"2010-07-21T15:18:04","date_gmt":"2010-07-21T13:18:04","guid":{"rendered":"http:\/\/joernhees.de\/blog\/?p=91"},"modified":"2016-09-28T20:40:45","modified_gmt":"2016-09-28T18:40:45","slug":"sort-python-dict-by-values","status":"publish","type":"post","link":"https:\/\/joernhees.de\/blog\/2010\/07\/21\/sort-python-dict-by-values\/","title":{"rendered":"Sort python dictionaries by values"},"content":{"rendered":"<p>Perhaps you already encountered a problem like the following one yourself:<br \/>\nYou have a large list of items (let&#8217;s say URIs for this example) and want to sum up how often they were viewed (or edited or&#8230; whatever). A small one-shot solution in python looks like the following and uses the often unknown <code>operator.itemgetter<\/code>:<\/p>\n<pre><code class=\"python\">import sys\nimport operator\nuriViews = {}\nfor line in sys.stdin:\n    uri, views = line.strip().split()\n    views = int(views)\n    uriViews[uri] = uriViews.get(uri, 0) - views\n    # why minus? could do + and use reverse=True below?\n    # right, but then items also get sorted in reverse, if views are\n    # the same (so Z would come first, a last)\nfor uri, views in sorted(uriViews.items(),\n                         key=operator.itemgetter(1,0)):\n    print -views, uri\n<\/code><\/pre>\n<p>This approach can be a lot <a href=\"http:\/\/writeonly.wordpress.com\/2008\/08\/30\/sorting-dictionaries-by-value-in-python-improved\/\">faster<\/a> than self written lambda functions called for every comparison or a list comprehension which turns around all tuples and then sorts it. Also in contrast to many other solutions you can find online this one uses <code>operator.itemgetter(1,0)<\/code>, which means that if two items have the same amount of views, their URIs are sorted alphabetically.<\/p>\n<p>Remember that this approach sorts the whole list in RAM, so you might want to chose other approaches in case your lists are getting huge.<br \/>\nFor further information you might want to read <a href=\"http:\/\/www.python.org\/dev\/peps\/pep-0265\/\">PEP-0265<\/a>, which also includes a hint what to do when you&#8217;re only interested in the Top 1000 for example (will only sort these top1000):<\/p>\n<pre><code class=\"python\">import heapq\ntop1000 = heapq.nlargest(1000, uriViews.iteritems(), itemgetter(1,0))\nfor uri,views in top1000:\n   print -views, uri\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Perhaps you already encountered a problem like the following one yourself: You have a large list of items (let&#8217;s say URIs for this example) and want to sum up how often they were viewed (or edited or&#8230; whatever). A small one-shot solution in python looks like the following and uses the often unknown operator.itemgetter: import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[2],"tags":[36,65,78,113,116,132,153,183,184],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pYA5n-1t","jetpack-related-posts":[{"id":166,"url":"https:\/\/joernhees.de\/blog\/2010\/07\/31\/urlencoding-in-python\/","url_meta":{"origin":91,"position":0},"title":"(URL)Encoding in python","date":"2010-07-31","format":false,"excerpt":"Well, encodings are a never ending story and whenever you don't want to waste time on them, it's for sure that you'll stumble over yet another tripwire. This time it is the encoding of URLs (note: even though related I'm not talking about the urlencode function). Perhaps you have seen\u2026","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":104,"url":"https:\/\/joernhees.de\/blog\/2010\/07\/22\/precision-recall-diagrams-including-fmeasure\/","url_meta":{"origin":91,"position":1},"title":"Precision-Recall diagrams including F-Measure height lines","date":"2010-07-22","format":false,"excerpt":"Today I was asked how to generate Recall-Precision diagrams including the f-measure values as height-lines from within python. Actually Gunnar was the one who had this idea quite a while ago, but constantly writing things into files, then loading them with his R code to visualize them, made me create\u2026","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"https:\/\/i2.wp.com\/joernhees.de\/blog\/wp-content\/uploads\/2010\/07\/RecallPrecisionDiagram-300x223.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":81,"url":"https:\/\/joernhees.de\/blog\/2010\/07\/19\/min-heap-in-python\/","url_meta":{"origin":91,"position":2},"title":"Min-Heap in Python","date":"2010-07-19","format":false,"excerpt":"I recently wanted to implement a small event system where events can have different priorities. So for example the event with highest priority (lowest value) should be handled first. Python comes with a heapq module which can transform a list into a heap in a way that it stays a\u2026","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":566,"url":"https:\/\/joernhees.de\/blog\/2014\/02\/25\/scientific-python-on-mac-os-x-10-9-with-homebrew\/","url_meta":{"origin":91,"position":3},"title":"Scientific Python on Mac OS X 10.9+ with homebrew","date":"2014-02-25","format":false,"excerpt":"Scientific python setup guide for Mac OS X 10.9 Mavericks with homebrew","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":314,"url":"https:\/\/joernhees.de\/blog\/2010\/12\/15\/python-unicode-doctest-howto-in-a-doctest\/","url_meta":{"origin":91,"position":4},"title":"Python unicode doctest howto in a doctest","date":"2010-12-15","format":false,"excerpt":"Another thing which has been on my stack for quite a while has been a unicode doctest howto, as I remember I was quite lost when I first tried to test encoding stuff in a doctest. So I thought the ultimate way to show how to do this would be\u2026","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":256,"url":"https:\/\/joernhees.de\/blog\/2010\/09\/21\/how-to-convert-hex-strings-to-binary-ascii-strings-in-python-incl-8bit-space\/","url_meta":{"origin":91,"position":5},"title":"How to convert hex strings to binary ascii strings in python (incl. 8bit space)","date":"2010-09-21","format":false,"excerpt":"As i come across this again and again: How do you turn a hex string like \"c3a4c3b6c3bc\" into a nice binary string like this: \"11000011 10100100 11000011 10110110 11000011 10111100\"? The solution is based on the Python 2.6 new string formatting: >>> \"{0:8b}\".format(int(\"c3\",16)) '11000011' Which can be decomposed into 4\u2026","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/posts\/91"}],"collection":[{"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":2,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions\/771"}],"wp:attachment":[{"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}