What is wrong in my list comprehension?

Chris Rebert clp2 at rebertia.com
Sun Feb 1 12:39:06 CET 2009


On Sun, Feb 1, 2009 at 3:22 AM, Hussein B <hubaghdadi at gmail.com> wrote:
> Hey,
> I have a log file that doesn't contain the word "Haskell" at all, I'm
> just trying to do a little performance comparison:
> ++++++++++++++
> from datetime import time, timedelta, datetime
> start = datetime.now()
> print start
> lines = [line for line in file('/media/sda4/Servers/Apache/
> Tomcat-6.0.14/logs/catalina.out') if line.find('Haskell')]

>From the help() for str.find:
find(...)
<snip>
    Return -1 on failure.

~ $ python
Python 2.6 (r26:66714, Nov 18 2008, 21:48:52)
[GCC 4.0.1 (Apple Inc. build 5484)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> bool(-1)
True

str.find() returns -1 on failure (i.e. if the substring is not in the
given string).
-1 is considered boolean true by Python.
Therefore, your list comprehension will contain all lines that don't
*start* with "Haskell" rather than all lines that don't *contain*
"Haskell".

Use `if "Haskell" in line` instead of `if line.find("Haskell")`. It's
even easier to read that way.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list