简体   繁体   English

python 3.7 中不区分大小写的字典?

[英]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?有没有默认具有不区分大小写的字典的 python 版本?

Ie, if I queried dict['value'] it would be the same as dict['VALUE'] ?即,如果我查询dict['value']它将与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.我只是问,因为我正在使用一些用 Python 3.7 编写的代码,但我正在使用 Python 3.8.5。 I could either just rewrite the code, or try a different version of Python - not sure which will take longer.我可以只重写代码,或者尝试不同版本的 Python - 不确定哪个需要更长时间。

Perhaps this is also a function of pandas, which went from pandas 1.0.4 to '1.1.3'.也许这也是 pandas 的 function,从 pandas 1.0.4 到 '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.我不知道任何以前版本的 Python 可以做到这一点,但您可能可以制作自己的字典类型来轻松完成此操作。

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() .这个想法是在您获取/设置字典值并将其替换为key.lower()时截取key You would want to do this for each capability of dict s that you use, eg, __delitem__() , __contains__() , etc.您可能希望为您使用的dict的每个功能执行此操作,例如__delitem__()__contains__()等。

Dictionaries are mutable unordered collections (they do not record element position or order of insertion) of key-value pairs.字典是键值对的可变无序 collections(它们不记录元素 position 或插入顺序)。 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.其他语言的字典也称为 hash 表或关联 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.用于键的数字类型遵循数字比较的常规规则:如果两个数字比较相等(例如 1 和 1.0),那么它们可以互换使用来索引同一个字典条目。

Reference: dict参考:字典

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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