Good use for itertools.dropwhile and itertools.takewhile

Vlastimil Brom vlastimil.brom at gmail.com
Tue Dec 4 16:08:34 EST 2012


2012/12/4 Nick Mellor <thebalancepro at gmail.com>:
> I love the way you guys can write a line of code that does the same as 20 of mine :)
> I can turn up the heat on your regex by feeding it a null description or multiple white space (both in the original file.) I'm sure you'd adjust, but at the cost of a more complex regex.
> Meanwhile takewith and dropwith are behaving themselves impeccably but my while loop has fallen over.
>
> Best,
> Nick
>> [...]
> --

Hi,
well, for what is it worth, both cases could be addressed quite
easily, with little added complexity - e.g.: make the description part
optional, allow multiple whitespace and enforce word boundary after
the product name in order to get rid of the trailing whitespace in it:

>>> re.findall(r"(?m)^([A-Z\s]+\b)(?:\s+(.*))?$", "CAPSICUM RED fresh from QLD\nCAPSICUM    RED   fresh from    Queensland\nCAPSICUM RED")
[('CAPSICUM RED', 'fresh from QLD'), ('CAPSICUM    RED', 'fresh from
 Queensland'), ('CAPSICUM RED', '')]
>>>

However, it's certainly preferable to use a solution you are more
comfortable with, e.g. the itertools one...

regards,
   vbr



More information about the Python-list mailing list