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

Discussion in 'Python' started by Steven Bethard, Mar 12, 2005.

  1. 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/Cookbook/Python/Recipe/303279
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303060
    > http://aspn.activestate.com/ASPN/Cookbook/Python/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/python-list/2005-January/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
    Steven Bethard, Mar 12, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francis Avila
    Replies:
    23
    Views:
    765
    Francis Avila
    Oct 30, 2003
  2. Francis Avila
    Replies:
    0
    Views:
    452
    Francis Avila
    Nov 2, 2003
  3. Ville Vainio

    Wishlist item: itertools.flatten

    Ville Vainio, Mar 11, 2005, in forum: Python
    Replies:
    19
    Views:
    648
    Christos TZOTZIOY Georgiou
    Mar 17, 2005
  4. Replies:
    2
    Views:
    377
    Steve Pope
    Sep 27, 2006
  5. bolega
    Replies:
    1
    Views:
    670
    Stan Bischof
    Mar 28, 2011
Loading...

Share This Page