Don't want to do the regexp test twice

Jp Calderone exarkun at intarweb.us
Thu Jul 24 21:45:17 EDT 2003


On Thu, Jul 24, 2003 at 11:44:57PM +0200, Egbert Bouwman wrote:
> [snip]
> 
> for line in mylist:
>     mat = pat.search(line)
>     if  ...:
>         ....
>     elif ...:
>         ....
>     elif mat:
>         ...
> but the test is relevant in only a relatively small number of cases.    
> And i would like to know if there exists a general solution
> for this kind of problem.

  People will tell you not to use this solution for the obvious reasons:

    for line in mylist:
        if ...:
            ....
        elif ...:
            ....
        elif [mat for mat in [pat.search(line)]][0]:
            # mat is now bound

  List comprehensions at work.

  Jp

-- 
"Pascal is Pascal is Pascal is dog meat."
                -- M. Devine and P. Larson, Computer Science 340





More information about the Python-list mailing list