case insensitive dictionary
vbgunz
vbgunz at gmail.com
Sat Nov 25 19:10:49 EST 2006
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/Cookbook/Python/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 :)
More information about the Python-list
mailing list