Popular recipes tagged "partition" but not "string"http://code.activestate.com/recipes/tags/partition-string/2013-01-21T16:20:17-08:00ActiveState Code RecipesSplit a sequence or generator using a predicate (Python) 2013-01-21T16:20:17-08:00Paul McGuirehttp://code.activestate.com/recipes/users/1377254/http://code.activestate.com/recipes/578416-split-a-sequence-or-generator-using-a-predicate/ <p style="color: grey"> Python recipe 578416 by <a href="/recipes/users/1377254/">Paul McGuire</a> (<a href="/recipes/tags/partition/">partition</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/splitting/">splitting</a>). Revision 3. </p> <p>Split a sequence or generator into two iterators, each iterating over the elements that either pass or fail a predicate function.</p> Generate the partitions of a set, by index (Python) 2010-04-30T11:07:30-07:00Anton Vredegoorhttp://code.activestate.com/recipes/users/2667360/http://code.activestate.com/recipes/577211-generate-the-partitions-of-a-set-by-index/ <p style="color: grey"> Python recipe 577211 by <a href="/recipes/users/2667360/">Anton Vredegoor</a> (<a href="/recipes/tags/combinatorics/">combinatorics</a>, <a href="/recipes/tags/partition/">partition</a>). </p> <p>Sets of only a few items already have many ways they can be partitioned into subsets. Therefore it can be useful to generate these partitions by index, like the partition class were some large list where one can just access element "i". Of course one should not compute the whole list in advance but compute the partitions on the fly. This recipe was originally extracted form a book by Kreher and Stinson. Over the years I came back to my code from time to time creating a new and hopefully more pythonic version each time I understood it better. My current take on it is that the algorithm looks a lot like creating a Pascals triangle in order to compute combinations. One just tries to find a way down the triangle to a specific element, each time subtracting the amounts the different positions in the triangle account for. It is also similar to finding indexed permutations of a set with elements occurring more than once. One of these days I will perhaps understand how all of this fits together. Until then I'll post code solving specific situations. </p> Partition an iterable into n lists (Python) 2009-05-30T16:35:27-07:00Ian Eloffhttp://code.activestate.com/recipes/users/2227021/http://code.activestate.com/recipes/576785-partition-an-iterable-into-n-lists/ <p style="color: grey"> Python recipe 576785 by <a href="/recipes/users/2227021/">Ian Eloff</a> (<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/partition/">partition</a>). </p> <p>This could also be easily modified to return n iterators, but was outside of my needs. Handy for splitting up the workload for use with multiple threads/processes.</p>