Popular Python recipes tagged "partition"http://code.activestate.com/recipes/langs/python/tags/partition/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>
string multi-partitioning (Python)
2010-03-26T20:03:59-07:00Michael Grünewaldhttp://code.activestate.com/recipes/users/4172244/http://code.activestate.com/recipes/577017-string-multi-partitioning/
<p style="color: grey">
Python
recipe 577017
by <a href="/recipes/users/4172244/">Michael Grünewald</a>
(<a href="/recipes/tags/partition/">partition</a>, <a href="/recipes/tags/string/">string</a>).
Revision 6.
</p>
<p>Works like the <code>partition</code> method of strings in Python2.5+, but has support for more than one delimiter.</p>
<p><em>Better description by Gabriel Genellina:</em></p>
<blockquote>
<p>Split the string at separator boundaries. The separators are searched from left to right, in the same order specified, and one after another. Unlike <code>partition</code>, all of them must be present (else <code>ValueError</code> is raised). Only one split per separator occurrence is done. Returns a list containing one more element than separators were given: first, text from beginning of the string up to (but not including) the first separator; the first separator itself; text between the first separator and the second one; the second separator; and so on. The last element contains all text following the last separator.</p>
</blockquote>
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>