case insensitive comparison operator ?

Discussion in 'Python' started by Will McGugan, Aug 18, 2004.

  1. Will McGugan

    Will McGugan Guest

    What is the best way to do a simple case insensitive comparison in Python?

    As far as I can tell, the only option is to do str1.lowercase() ==
    str2.lowercase() (or uppercase). But that strikes me as quite verbose
    for such a common operation, and possibly inneficient since it would
    create and destroy 2 tempory objects. Perhaps there should be a
    dedicated function in the string object, or (+1) an operator?

    Apologies if I have missed something. I haven't done a great deal of
    work int Python yet.


    Will McGugan
     
    Will McGugan, Aug 18, 2004
    #1
    1. Advertising

  2. Will McGugan

    Peter Hansen Guest

    Will McGugan wrote:

    > What is the best way to do a simple case insensitive comparison in Python?
    >
    > As far as I can tell, the only option is to do str1.lowercase() ==
    > str2.lowercase() (or uppercase).


    Actually, it's str1.lower() == str2.lower().

    > But that strikes me as quite verbose


    Luckily it's not that verbose after all! ;-)

    > for such a common operation,


    Nor that common! If you need to do this more than once in an
    application, you can easily create a simple compareCaseInsensitive()
    function that hides the above "excessive verbosity" and you'll
    never notice it again. Furthermore, in most cases you've already
    stored one of those two strings, so if you lower() it when you
    store it, you only need to do "str1 == str2.lower()" when you
    are comparing later on. At least that's the use case I've
    seen to be "common", and I've written more Python code than you
    have so there. ;-)

    > and possibly inneficient since it would create and destroy 2
    > tempory objects.


    Python creates and destroys a lot more temporary objects than you
    might imagine, and still gets by. In fact, the function calls
    involved probably have much higher overhead (from setting up
    the stack frame) than the object creation... Python often upsets
    conventional ideas of where hotspots will be.

    > Apologies if I have missed something. I haven't done a great deal of
    > work int Python yet.


    Worrying about possible inefficiencies before you've even done
    much coding in a language is a clear case of premature
    optimization. Just write some code, learn to use and love Python,
    and if you still really want this, come back later and suggest
    it again. ;-)

    -Peter
     
    Peter Hansen, Aug 18, 2004
    #2
    1. Advertising

  3. Will McGugan

    Max M Guest

    Will McGugan wrote:

    > What is the best way to do a simple case insensitive comparison in Python?
    >
    > As far as I can tell, the only option is to do str1.lowercase() ==
    > str2.lowercase() (or uppercase). But that strikes me as quite verbose
    > for such a common operation, and possibly inneficient since it would
    > create and destroy 2 tempory objects. Perhaps there should be a
    > dedicated function in the string object, or (+1) an operator?



    How else could it be done?

    You could wrap it in a function, and reuse the code if you want to. That
    way you can imagine that it never happens ;-)

    def cic(x,y);
    "Case insensitive compare"
    return x.lower() == y.lower()

    But you cannot really do it any more efficient.

    Forget your worries. It's probably just a case of premature optimisation.

    Naturally there are special cases where you must do it more efficiently.
    But they would probably need to be solved based on the specific problem.

    regards Max M
     
    Max M, Aug 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tee
    Replies:
    3
    Views:
    7,878
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  2. Replies:
    1
    Views:
    2,521
    Mark P
    Apr 6, 2007
  3. Mosfet
    Replies:
    22
    Views:
    1,764
    kwikius
    Jul 23, 2007
  4. Brad
    Replies:
    2
    Views:
    106
  5. Xah Lee
    Replies:
    4
    Views:
    1,020
Loading...

Share This Page