By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

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

P: n/a
ge*******@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
Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.