Good use for itertools.dropwhile and itertools.takewhile
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Dec 4 16:13:01 EST 2012
Ian,
For the sanity of those of us reading this via Usenet using the Pan
newsreader, could you please turn off HTML emailing? It's very
distracting.
Thanks,
Steven
On Tue, 04 Dec 2012 12:37:38 -0700, Ian Kelly wrote:
[...]
> <div class="gmail_quote">On Tue,
> Dec 4, 2012 at 11:48 AM, Alexander Blinne <span dir="ltr"><<a
> href="mailto:news at blinne.net"
> target="_blank">news at blinne.net</a>></span> wrote:<br><blockquote
> class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc
> solid;padding-left:1ex">
>
> Am 04.12.2012 19:28, schrieb DJC:<br> <div class="im">>>>>
> (i for i,v in enumerate(w) if v.upper() != v).next()<br> > Traceback
> (most recent call last):<br> > File "<stdin>", line
> 1, in <module><br> > AttributeError: 'generator' object
> has no attribute 'next'<br> <br>
> </div>Yeah, i saw this problem right after i sent the posting. It now
> is<br> supposed to read like this<br>
> <div class="im"><br>
> >>> def split_product(p):<br> ... w = p.split("
> ")<br> </div>... j = next(i for i,v in enumerate(w) if
> v.upper() != v)<br> <div class="im">... return "
> ".join(w[:j]), "
> ".join(w[j:])<br></div></blockquote></div><br>It still fails if the
> product description is empty.<br><br>>>>
> split_product("CAPSICUM RED")<br>
>
> Traceback (most recent call last):<br> File "<stdin>",
> line 1, in <module><br> File "<stdin>", line 3,
> in split_product<br>StopIteration<br><br>I'm not meaning to pick on
> you; some of the other solutions in this thread also fail in that
> case.<br>
>
> <br>>>> re.findall(r"(?m)^([A-Z\s]+) (.+)$",
> "CAPSICUM RED")<br>[('CAPSICUM',
> 'RED')]<br><br>>>> prod_desc("CAPSICUM RED")
> # the second version from Neil's post<br>
>
> Traceback (most recent call last):<br> File "<stdin>",
> line 1, in <module><br> File "<stdin>", line 14,
> in prod_desc<br>IndexError: string index out of range<br><br>
--
Steven
More information about the Python-list
mailing list