Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > case insensitive comparison operator ?

Reply
Thread Tools

case insensitive comparison operator ?

 
 
Will McGugan
Guest
Posts: n/a
 
      08-18-2004
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
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      08-18-2004
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
 
Reply With Quote
 
 
 
 
Max M
Guest
Posts: n/a
 
      08-18-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
check file exists with case sensitive on a case insensitive filesystem Xah Lee Perl Misc 4 04-05-2009 11:21 PM
std::string and case insensitive comparison Mosfet C++ 22 07-23-2007 01:37 PM
case insensitive find on case sensitive stl map benhoefer@gmail.com C++ 1 04-06-2007 08:42 PM
how to case select with case-insensitive string ? Tee ASP .Net 3 06-23-2004 07:40 PM
String: case insensitive comparison Brad Ruby 2 05-18-2004 08:24 PM



Advertisments