473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

case insensitive comparison operator ?

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
Jul 18 '05 #1
2 11537
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
Jul 18 '05 #2
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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Relaxin | last post by:
Is there a way to make Postgresql case-INSENSITIVE? Thanks
4
by: Dario Ottaviano | last post by:
I use postgres on a window server (IIS 5.1) Is there anybody that knows if is possible to make postgres no case sensitive in the manipulating data into tables/views? Thank you. D.Ottaviano
1
by: Ron James | last post by:
I have a Listbox containing strings. When calling Contains (), I would like a case insensitive comparison. (I don't want to add newfile.txt to a Listbox containing NewFile.txt). I'm...
7
by: Jan Nielsen | last post by:
Hi all I am learning Asp.Net using Vb.net (VS 2002). I found the following example in a book: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
6
by: anonieko | last post by:
Is this a long way to compare strings ignoring the case? if ( string.Compare(wsUri.Host, "localHost", System.StringComparison.OrdinalIgnoreCase) == 0 ) { // etc. }
7
by: Adrian | last post by:
Hi, I want a const static std::set of strings which is case insensitive for the values. So I have the following which seems to work but something doesnt seem right about it. Is there a better...
1
by: Coder Guy | last post by:
Is it possible to do a case insensitive comparison of individual characters? I tried: Char.ToLowerInvariant(charA).CompareTo(Char.ToLowerInvariant(charB)) And though it works for the most...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
0
by: Chris Rebert | last post by:
On Thu, Sep 4, 2008 at 3:37 PM, Robert Dailey <rcdailey@gmail.comwrote: Then store the string in its original case in the value part of the key-value pair: stage_map = (key,whatever) - Chris
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.