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" 7 2081
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/
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 :)
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.
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/
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: noah |
last post by:
Can anyone think of a way to make array keys case insensitive?
An option on any recent PHP would be to convert all array keys to
lowercase
using the standard array_change_key_case() function.
As...
|
by: Elbert Lev |
last post by:
Hi!
Here is the problem:
I have a dictionary. Keys are strings. How to make dictionary lookup
case insensitive?
In other words:
If dict = {'First":"Bob", "Last":"Tom"}, dict should return...
|
by: Ville Vainio |
last post by:
I need a dict (well, it would be optimal anyway) class that stores the
keys as strings without coercing the case to upper or lower, but still
provides fast lookup (i.e. uses hash table).
>> d...
|
by: Tumurbaatar S. |
last post by:
I need something like Scripting.Dictionary
but with case insensitive keys, i.e.
dict.Item("mykey") and dict.Item("MyKeY") should
be identical. Any ideas?
|
by: William Stacey [MVP] |
last post by:
Trying to figure out Dictionary<> and using CaseInsensitive Comparer<> like
I did with normal Hashtable. The Hashtable can take a case insenstive
Comparer and a Case insensitive HashCode provider....
|
by: Mark Rae |
last post by:
Hi,
In v1.x, I used to use the following code to create a case-insensitive
Hashtable:
Hashtable MyHashtable = new
Hashtable(CaseInsensitiveHashCodeProvider.Default,...
|
by: Mark Rae |
last post by:
Hi,
Is it possible to create a case-insensitive List<stringcollection?
E.g.
List<stringMyList = new List<string>;
MyList.Add("MyString");
So that:
|
by: pamela fluente |
last post by:
Hi
I am doing something like:
Dim MiDict As New System.Collections.Generic.Dictionary(Of String,
MyObject)
how should I change the above so that the string comparison (key) is
done using...
|
by: J. Cliff Dyer |
last post by:
Please keep the discussion on-list.
On Fri, 2008-09-05 at 15:36 +0200, Maric Michaud wrote:
That does not get the same behavior as the OP's original solution. The
OP's solution retrieves the...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
| |