{"id":101,"date":"2010-07-23T14:01:28","date_gmt":"2010-07-23T12:01:28","guid":{"rendered":"http:\/\/joernhees.de\/blog\/?p=101"},"modified":"2016-09-28T23:38:45","modified_gmt":"2016-09-28T21:38:45","slug":"itertools","status":"publish","type":"post","link":"https:\/\/joernhees.de\/blog\/2010\/07\/23\/itertools\/","title":{"rendered":"Itertools"},"content":{"rendered":"<p>Just recently came across the python <a href=\"http:\/\/docs.python.org\/library\/itertools.html\"><code>itertools<\/code> &#8220;tools for efficient looping&#8221;<\/a> again. Generators have the advantage of not creating the whole list on definition, but on demand (in contrast to e.g., list comprehensions). Really worth a look:<\/p>\n<pre><code class=\"python\">import itertools as it\ng = it.cycle(\"abc\") # a generator\ng.next() # a\ng.next() # b\ng.next() # c\ng.next() # a\ng.next() # b\n# ... and so on\n\ng = it.cycle(\"abcde\")\nh = it.cycle(\"1234\")\ngh = it.izip(g,h) # iterzips n iterables together\ngh.next() # (a,1)\ngh.next() # (b,2)\n# ... think about what this means with primes\ngh.next() # (e,4)\ngh.next() # (a,1)\n# ...\n<\/code><\/pre>\n<p>Also very nice are the combinatoric generators:<\/p>\n<pre><code class=\"python\">it.product('ABCD',  repeat=2) # AA AB AC AD BA BB BC BD\n                              # CA CB CC CD DA DB DC DD\nit.permutations('ABCD',  2)   # AB AC AD BA BC BD CA CB CD DA DB DC\nit.combinations('ABCD',  2)   # AB AC AD BC BD CD\nit.combinations_with_replacement('ABCD',  2) # AA AB AC AD BB BC BD CC CD DD\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Just recently came across the python itertools &#8220;tools for efficient looping&#8221; again. Generators have the advantage of not creating the whole list on definition, but on demand (in contrast to e.g., list comprehensions). Really worth a look: import itertools as it g = it.cycle(&#8220;abc&#8221;) # a generator g.next() # a g.next() # b g.next() # [&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":[28,79,118,127,132,183],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pYA5n-1D","jetpack-related-posts":[{"id":277,"url":"https:\/\/joernhees.de\/blog\/2010\/10\/31\/setting-up-a-local-dbpedia-mirror-with-virtuoso\/","url_meta":{"origin":101,"position":0},"title":"Setting up a local DBpedia mirror with Virtuoso","date":"2010-10-31","format":false,"excerpt":"So you're the guy who is allowed to setup a local DBpedia mirror for your work group? OK, today is your lucky day and you're in the right place. I hope you'll be able to benefit from my hours of trials and errors ;)","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":81,"url":"https:\/\/joernhees.de\/blog\/2010\/07\/19\/min-heap-in-python\/","url_meta":{"origin":101,"position":1},"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":442,"url":"https:\/\/joernhees.de\/blog\/2012\/05\/25\/setting-up-a-local-dbpedia-3-7-mirror-with-virtuoso-6-1-5\/","url_meta":{"origin":101,"position":2},"title":"Setting up a local DBpedia 3.7 mirror with Virtuoso 6.1.5+","date":"2012-05-25","format":false,"excerpt":"Newer version available: Setting up a Linked Data mirror from RDF dumps (DBpedia 2015-04, Freebase, Wikidata, LinkedGeoData, ...) with Virtuso 7.2.1 and Docker (optional) Nearly 1.5 years after i initially published a post about how to setup a local DBpedia mirror i recently revisited the problem myself to setup a\u2026","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":584,"url":"https:\/\/joernhees.de\/blog\/2014\/04\/23\/setting-up-a-local-dbpedia-3-9-mirror-with-virtuoso-7\/","url_meta":{"origin":101,"position":3},"title":"Setting up a local DBpedia 3.9 mirror with Virtuoso 7","date":"2014-04-23","format":false,"excerpt":"Newer version available: Setting up a Linked Data mirror from RDF dumps (DBpedia 2015-04, Freebase, Wikidata, LinkedGeoData, ...) with Virtuso 7.2.1 and Docker (optional) I just found this aged post in my drafts folder, maybe someone will still like it... So you're the guy who is allowed to setup a\u2026","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":731,"url":"https:\/\/joernhees.de\/blog\/2015\/11\/23\/setting-up-a-linked-data-mirror-from-rdf-dumps-dbpedia-2015-04-freebase-wikidata-linkedgeodata-with-virtuoso-7-2-1-and-docker-optional\/","url_meta":{"origin":101,"position":4},"title":"Setting up a Linked Data mirror from RDF dumps (DBpedia 2015-04, Freebase, Wikidata, LinkedGeoData, ...) with Virtuoso 7.2.1 and Docker (optional)","date":"2015-11-23","format":false,"excerpt":"So you're the guy who is allowed to setup a local DBpedia mirror or more generally a local Linked Data mirror for your work group? OK, today is your lucky day and you're in the right place. I hope you'll be able to benefit from my many hours of trials\u2026","rel":"","context":"In &quot;Coding&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":91,"url":"https:\/\/joernhees.de\/blog\/2010\/07\/21\/sort-python-dict-by-values\/","url_meta":{"origin":101,"position":5},"title":"Sort python dictionaries by values","date":"2010-07-21","format":false,"excerpt":"Perhaps you already encountered a problem like the following one yourself: You have a large list of items (let's say URIs for this example) and want to sum up how often they were viewed (or edited or... whatever). A small one-shot solution in python looks like the following and uses\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\/101"}],"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=101"}],"version-history":[{"count":2,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":799,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions\/799"}],"wp:attachment":[{"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joernhees.de\/blog\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}