Good use for itertools.dropwhile and itertools.takewhile

Neil Cerutti neilc at norwich.edu
Wed Dec 5 13:16:04 EST 2012


On 2012-12-05, Nick Mellor <thebalancepro at gmail.com> wrote:
> Hi Neil,
>
> Here's some sample data. The live data is about 300 minor
> variations on the sample data, about 20,000 lines.

Thanks, Nick.

This slight variation on my first groupby try seems to work for
the test data.

def prod_desc(s):
    prod = []
    desc = []
    for k, g in itertools.groupby(s.split(),
            key=lambda w: any(c.islower() for c in w)):
        if prod or k:
            desc.extend(g)
        else:
            prod.extend(g)
    return [' '.join(prod), ' '.join(desc)]

-- 
Neil Cerutti



More information about the Python-list mailing list