Case-insensitive string compare?

J. Cliff Dyer jcd at sdf.lonestar.org
Fri Sep 5 10:00:39 EDT 2008


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
> 
> 
> 




More information about the Python-list mailing list