parenthesis
Michele Simionato
mis6 at pitt.edu
Tue Nov 5 13:42:57 EST 2002
Lee Harr <missive at frontiernet.net> wrote in message news:<use8fo2je674ad at corp.supernews.com>...
> In article <2259b0e2.0211041224.7c24635b at posting.google.com>,
> Michele Simionato wrote:
> > Suppose I want to parse the following expression:
> >
> >>>> exp='(a*(b+c*(2-x))+d)+f(s1)'
> >
> > I want to extract the first part, i.e. '(a*(b+c*(2-x))+d)'.
> >
>
>
> n = 0
> for c in range(len(exp)):
> l = exp[c]
> if l == '(':
> n += 1
> elif l == ')':
> n -= 1
> if not n:
> out = exp[0:c+1]
> break
> print out
This is not the same that I did, indeed. Even if the logic is essentially the
same, you don't need to create the out list. It turns out the your approach
is 35% faster than mine, contrary to my naive expectations !
There-are-lies-damned-lies-and-benchmarks-ly yours,
Michele
More information about the Python-list
mailing list