Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   wishlist item: itertools.partition (WAS: Wishlist item: itertools.flatten) (http://www.velocityreviews.com/forums/t342575-wishlist-item-itertools-partition-was-wishlist-item-itertools-flatten.html)

Steven Bethard 03-12-2005 07:50 PM

wishlist item: itertools.partition (WAS: Wishlist item: itertools.flatten)
 
gene.tani@gmail.com wrote:
> window / cons / fencepost / slice functions: +1
>
> (with a flag to say if you want to truncate or pad incomplete tuples
> at end of input sequence.
>
> http://aspn.activestate.com/ASPN/Coo.../Recipe/303279
> http://aspn.activestate.com/ASPN/Coo.../Recipe/303060
> http://aspn.activestate.com/ASPN/Coo.../Recipe/347689
>
> Probably more recipes in there, (and not CPAN-ish yet) but multiple
> submissions bespeak a certain need, i think.


Yes, also worth noting is the thread:

http://mail.python.org/pipermail/pyt...ry/263004.html

which concludes with:

from itertools import islice, chain, repeat

def partition(iterable, part_len):
itr = iter(iterable)
while 1:
item = tuple(islice(itr, part_len))
if len(item) < part_len:
raise StopIteration
yield item

def padded_partition(iterable, part_len, pad_val=None):
padding = repeat(pad_val, part_len-1)
itr = chain(iter(iterable), padding)
return partition(itr, part_len)

STeVe


All times are GMT. The time now is 12:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.