473,385 Members | 1,587 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,385 software developers and data experts.

Re: Case-insensitive string compare?

Please keep the discussion on-list.

On Fri, 2008-09-05 at 15:36 +0200, Maric Michaud wrote:
Le Friday 05 September 2008 14:33:22 J. Clifford Dyer, vous avez écrit :
On Thu, 2008-09-04 at 18:48 -0500, Robert Dailey wrote:
Thanks everyone for your help. I'm not opposed to using [key.lower()
for key in stage_map] at all, I was just curious to see if there were
any cleaner alternatives. It looks like that is what I'll be using.
I'm not familiar with how python works internally, but coming from C++
it seems like "remaking" the map would be slow. However, speed is not
my main concern in my particular situation, I'm just curious to learn
more about python.
You should be opposed to that particular solution. You have just taken
a dictionary lookup (very fast) and turned it into a list traversal
(slow). Even if speed isn't your main concern, this is an unnecessary
de-optimization. You are deliberately slowing down your program to
avoid a slightly more verbose lookup later. Your data structure might
as well be a list of tuples to begin with, to avoid creating a new list.
You have effectively made your keys useless as keys.

If your lookups need to be case insensitive, make the key lower case,
and store the cased version in the value, whether as a tuple or a dict
(depending on whether you want named access).

d = {
'foo': {'key': 'Foo', 'value': 'val1'}
'spam': {'key': 'sPAm', 'value': 'val2'}
}

search = 'FOO'.lower()
if search in d:
result = d[search]
key = result['key']
value = result['value']

The only reason I wouldn't use this solution is if you expect to have
keys that will be identical when made lowercase, but if you're doing
case-insensitive lookup, you obviously don't expect this to be an issue.

The OP has already said the keys are case-sensitive, so this is not an option,
the only way to do it fast is to index upon insertion all keys in another
dict, so you get in final :
d = { "kEy1" : 1, "Key1" : 2}
indexes = { "key1" : ["kEy1", "Key1" ] }
That does not get the same behavior as the OP's original solution. The
OP's solution retrieves the first matching value only. Mine gets one
arbitrary matching value, determined by the algorithm used for
constructing the search dict (not specified in my solution). What the
OP actually said was that he can't throw away the case. You assume that
this means there would be multiple entries distinguished only by case.
That might be true, but given his initial solution, it seemed to me more
likely that he wants to keep the case for display purposes, as in a
dictionary like this:

{
"Michaud": "Maric",
"Dyer": "Cliff",
"D'Amato": "Alphonse",
}

To the OP: If you are looking for multiple matches for a given
case-insensitive key, check out the recent thread on "Python multimap",
for further discussion elaborating on how to map one key to many values
in a dictionary-like object.

At that point, it's probably worth factoring out your solution to a
separate class, so you can work with a clean interface, rather than
repeating the ugly details every time you use it.

Cheers,
Cliff
Cheers,
Cliff
--
http://mail.python.org/mailman/listinfo/python-list


Sep 5 '08 #1
0 4986

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

Similar topics

1
by: ST | last post by:
Hi, I'm trying to debug someone else's code, and I'm going thru this Select Case statement. I'm having problems with the "OTHER" case...in that when the first line of the case is false, it jumps...
2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
7
by: Lauren Quantrell | last post by:
Is there any speed/resource advantage/disadvantage in using Select Case x Case 1 Case 2 etc. many more cases... End Select VS.
10
by: Chih-Hsu Yen | last post by:
I encountered a strange problem about switch-case statement. switch(cmd) { case 1: statements; break; case 2: statements; break; ... .... case 11: S1; S2; S3; statements;
5
by: Frederick Dean | last post by:
Hi,guys! I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I meet a weird case: bool Postorder::next() { switch (pc) case START: while (true) if (!child()) { pc = LEAF; return...
21
by: markpapadakis | last post by:
I was checking out the C-FAQ and read here ( http://c-faq.com/misc/nonconstcase.html ) that: " case labels are limited to single, constant, integral expression ". However, I have been using...
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
17
by: Navodit | last post by:
So I have some code like: if (document.Insurance.State.selectedIndex == 1) { ifIll(); } else if (document.Insurance.State.selectedIndex == 2) { elseKan(); }
24
by: clockworx05 | last post by:
Hey guys i have this program that i need to write for class. here are the instructions: Write a function called foo that asks the user for their age. Pass the age value to a function called...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.