473,467 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 5002

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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.