conditionals in lambdas?

Alex Martelli aleaxit at yahoo.com
Fri Nov 3 17:13:45 EST 2000


"Michael P. Soulier" <msoulier at nortelnetworks.com> wrote in message
news:8tv6if$m79$1 at bmerhc5e.ca.nortel.com...
    [snip]
>     Thanks. I guess I'm still in that Perl mentality where I'm trying to
do
> many things on one line.

Nothing intrinsically wrong with that.

>     I was looking for a simple way of grabbing all lines that began with a
> the string #LOADDATA. In perl I'd do this:
>
> open (FILE, "file") or die "Can't open file: $!";
> @contents = grep { /^\#LOADDATA/ } <FILE>;

SO verbose?  Tch...!  Python 2...:

    contents = [x for x in open("file").readlines()
        if x.startswith('#LOADDATA')]

(you CAN cram it onto 1 line, but 1.5 read better:-).


Alex






More information about the Python-list mailing list