By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

case insensitive dictionary

P: n/a
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option. I found some work done by others
to do that at:

http://aspn.activestate.com/ASPN/Coo.../Recipe/283455

but the problem with that approach is that they lowercase the keys
immediately when you create the dictionary and so the true identity of
the key is lost.

Of course, I can subclass it and save a copy of the "real" key but
that's kind of messcy.

In other words:

If I have:

pets=caselessDict()

pets["Cat"] = 3
pets["Dog"] = 2

I would like to see:

pets["cat"] prints 3
pets["DOG"] prints 2

but

print pets.keys()

should print:

"Cat", "Dog"

not:

"cat", "dog"

Nov 25 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
John Henry wrote in news:1164494606.514366.124810
@l39g2000cwd.googlegroups.com in comp.lang.python:
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option.
class idict( dict ):

class __istr( str ):

def __eq__( self, other ):
return self.lower() == other.lower()

def __hash__( self ):
return self.lower().__hash__()

def __setitem__( self, k, v ):
dict.__setitem__( self, idict.__istr( k ), v )

d = idict( a = 1, b = 2 )
d['A'] = 3

print d

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Nov 25 '06 #2

P: n/a

John Henry wrote:
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option. I found some work done by others
to do that at:

http://aspn.activestate.com/ASPN/Coo.../Recipe/283455

but the problem with that approach is that they lowercase the keys
immediately when you create the dictionary and so the true identity of
the key is lost.

Of course, I can subclass it and save a copy of the "real" key but
that's kind of messcy.

In other words:

If I have:

pets=caselessDict()

pets["Cat"] = 3
pets["Dog"] = 2

I would like to see:

pets["cat"] prints 3
pets["DOG"] prints 2

but

print pets.keys()

should print:

"Cat", "Dog"

not:

"cat", "dog"
You can try to title-case the list returned with keys() like so:
print [x.title() for x in pets.keys()]

Not a perfect solution but you get what you want... just an idea :)

Nov 25 '06 #3

P: n/a
John Henry napisal(a):
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option. I found some work done by others
to do that at:

http://aspn.activestate.com/ASPN/Coo.../Recipe/283455

but the problem with that approach is that they lowercase the keys
immediately when you create the dictionary and so the true identity of
the key is lost.

Of course, I can subclass it and save a copy of the "real" key but
that's kind of messcy.

In other words:

If I have:

pets=caselessDict()

pets["Cat"] = 3
pets["Dog"] = 2

I would like to see:

pets["cat"] prints 3
pets["DOG"] prints 2

but

print pets.keys()

should print:

"Cat", "Dog"

not:

"cat", "dog"
How about:
>>class Dictstr(str):
.... def __hash__(self):
.... return str.__hash__(self.lower())
.... def __eq__(self,other):
.... return self.lower()==other.lower()
....
>>class dictt(dict):
.... def __setitem__(self, key, value):
.... if isinstance(key, str):
.... dict.__setitem__(self, Dictstr(key), value)
.... else:
.... dict.__setitem__(self, key, value)
.... def __getitem__(self, key):
.... if isinstance(key, str):
.... return dict.__getitem__(self, Dictstr(key))
.... else:
.... return dict.__getitem__(self, key)
....
>>d=dictt()
d[1]=1
d['Cat']='Cat'
d['Dog']='Dog'
d['dOg']
'Dog'
>>d.keys()
[1, 'Dog', 'Cat']

Note that you would need to redefine also __init__, __contains__ and
other methods.

Nov 26 '06 #4

P: n/a
I don't think that's sufficient. See how many methods the author of
http://aspn.activestate.com/ASPN/Coo.../Recipe/283455 had to
redefine.

Rob Williscroft wrote:
John Henry wrote in news:1164494606.514366.124810
@l39g2000cwd.googlegroups.com in comp.lang.python:
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option.

class idict( dict ):

class __istr( str ):

def __eq__( self, other ):
return self.lower() == other.lower()

def __hash__( self ):
return self.lower().__hash__()

def __setitem__( self, k, v ):
dict.__setitem__( self, idict.__istr( k ), v )

d = idict( a = 1, b = 2 )
d['A'] = 3

print d

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Nov 26 '06 #5

P: n/a
John Henry wrote:
print pets.keys()

should print:

"Cat", "Dog"
If you do:

Pypets['Cat'] = 2
Pypets['Dog'] = 3
Pypets['DOG'] = 4
Pypets['cat'] += 5
Pypets.keys()

What should the result be?

"['Cat', 'Dog']" or "['cat', 'DOG']"?
That is to say, if you use a new case in redefining the values of your
case insensitive dictionary, does the key take on the new case, or will
it always and forever be the case originally given to it?

Cheers,
Cliff
Nov 27 '06 #6

P: n/a

John Henry wrote:
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option. I found some work done by others
to do that at:

http://aspn.activestate.com/ASPN/Coo.../Recipe/283455

but the problem with that approach is that they lowercase the keys
immediately when you create the dictionary and so the true identity of
the key is lost.
A later version of this class does give you access to the original case
:

http://www.voidspace.org.uk/python/a...shtml#caseless

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Nov 27 '06 #7

P: n/a
I believe that if you redefine the value, the key should not change.
So, yes, I would expect that they value of the key to remain as they
were.
J. Clifford Dyer wrote:
John Henry wrote:
print pets.keys()

should print:

"Cat", "Dog"

If you do:

Pypets['Cat'] = 2
Pypets['Dog'] = 3
Pypets['DOG'] = 4
Pypets['cat'] += 5
Pypets.keys()

What should the result be?

"['Cat', 'Dog']" or "['cat', 'DOG']"?
That is to say, if you use a new case in redefining the values of your
case insensitive dictionary, does the key take on the new case, or will
it always and forever be the case originally given to it?

Cheers,
Cliff
Nov 27 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.