简体   繁体   中英

Case insensitive dicts in python 3.7?

This is probably a naive question from a programming languages standpoint (the answer must be no).

Was there ever a version of python that had case-insensitive dicts by default?

Ie, if I queried dict['value'] it would be the same as dict['VALUE'] ?

I just ask because I am working with some code written in Python 3.7, but I am working in Python 3.8.5. I could either just rewrite the code, or try a different version of Python - not sure which will take longer.

Perhaps this is also a function of pandas, which went from pandas 1.0.4 to '1.1.3'. I will check more on this.

I don't know of any previous version of Python that did this, but you could probably make your own dictionary type that does it pretty easily.

class UncasedDict(dict):                                                        
    def __getitem__(self, key):                                                 
        if isinstance(key, str):                                                
            key = key.lower()                                                   
        return super().__getitem__(key)                                         
                                                                                
    def __setitem__(self, key, value):                                          
        if isinstance(key, str):                                                
            key = key.lower()                                                   
        return super().__setitem__(key, value)                                  
                                                                                
                                                                                
d = UncasedDict()                                                               
                                                                                
d["hello"] = 1                                                                  
print(f'{d["hello"]=}')                                                         
print(f'{d["helLo"]=}')                                                         
                                                                                
d["GOODBYE"] = 2                                                                
print(f'{d["GOODBYE"]=}')                                                       
print(f'{d["GoOdByE"]=}')  

                                                 

# d["hello"]=1
# d["helLo"]=1
# d["GOODBYE"]=2
# d["GoOdByE"]=2

The idea is to just intercept key when you get/set the dictionary values and replace it with key.lower() . You would want to do this for each capability of dict s that you use, eg, __delitem__() , __contains__() , etc.

Dictionaries are mutable unordered collections (they do not record element position or order of insertion) of key-value pairs. Keys within the dictionary must be unique and must be hashable. That includes types like numbers, strings and tuples. Lists and dicts can not be used as keys since they are mutable. Dictionaries in other languages are also called hash tables or associative arrays.

Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as 1 and 1.0) then they can be used interchangeably to index the same dictionary entry.

Reference: dict

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM