Algorithm help per favore

Skip Montanaro skip at pobox.com
Wed Jun 18 17:11:43 EDT 2003


    David> It's tempting to use "not L[0]" instead (since what I want is an
    David> object that's not equal to L[0]) but that fails when L is empty.

    Skip> How about "not L[0:1]"?  Since L is a sequence (right?), "not
    Skip> L[0:1]" will be True if L is empty and False otherwise.

    David> That fails to produce an object that is guaranteed to be unequal
    David> to L[0] when L is nonempty.

Ah, right.  I missed the original spec and was only considering making "not
L[0]" blow up.  What about:

    >>> L = []
    >>> L and not L[0] or True
    True
    >>> L = [False]
    >>> L and not L[0] or True
    True
    >>> (L and not L[0] or True) == L[0]
    False

Of course, at this point "lambda:0" is looking better and better. ;-)

Skip





More information about the Python-list mailing list