naive string question

Létezõ letezo at fw.hu
Tue Mar 4 20:14:45 EST 2003


Your problem can be solved with an impressive list comprehension:

b= [(i,p) for i,c,p in zip(range(1,len(a)),a[1:],a[:-1]) if c!=p]

The above solution is more pythonical, but could be slower. I did not
measured their speed.

b=[]
for i in range(1, len(a)):
    if a[i] != a[i-1]:
        b.append((i, a[i-1]))

Best wishes: Viktor

============================================

----- Original Message -----
From: "Micha³ Kurowski" <mkur at poczta.gazeta.pl>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Wednesday, March 05, 2003 1:41 AM
Subject: naive string question


> Hi,
>
> I'm looking for a fast and elegant way of mapping a string to a list
> based on changes in the string (something like 'aaa-ab-a-bbbaa').
>
> So far I've got this:
>
> b= []
> for i in range(1, len(a)):
>     if a[i] != a[i-1]:
>         b.append((i, a[i-1]))
>
> It's truly the first thing that came to my mind.
> (Perhaps a bit influenced by not so good performance-wise
> experience with functional programming tools ;-).
>
> Something better highly appreciated.
>
> Cheers,
>
> --
> Michal Kurowski
> <mkur at poczta.gazeta.pl>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>







More information about the Python-list mailing list