Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > wishlist item: itertools.partition (WAS: Wishlist item: itertools.flatten)

Thread Tools

wishlist item: itertools.partition (WAS: Wishlist item: itertools.flatten)

Steven Bethard
Posts: n/a
      03-12-2005 Removed) 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.
> 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:

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)

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefox/TBird wishlist: printing font Firefox 0 03-14-2006 01:38 PM
"Holiday Wishlist 2006" at XYZ Computing Silverstrand Front Page News 7 11-21-2005 01:44 AM
wishlist? Decrufting bookmark/fav manager Splibbilla Firefox 3 11-13-2005 03:03 AM
FireFox wishlist - add Newbie4Ever Firefox 1 09-26-2005 07:56 AM
Wishlist for a 10D successor? Lionel Digital Photography 15 08-23-2003 09:54 PM