Case insensitive dictionary

后端 未结 10 2453
栀梦
栀梦 2020-11-27 14:47

I\'d like my dictionary to be case insensitive.

I have this example code:

text = \"practice changing the color\"

words = {\'color\': \'colour\',
            


        
10条回答
  •  我在风中等你
    2020-11-27 15:22

    If I understand you correctly and you want a way to key dictionaries in a non case-sensitive fashion, one way would be to subclass dict and overload the setter / getter:

    class CaseInsensitiveDict(dict):
        def __setitem__(self, key, value):
            super(CaseInsensitiveDict, self).__setitem__(key.lower(), value)
    
        def __getitem__(self, key):
            return super(CaseInsensitiveDict, self).__getitem__(key.lower())
    

提交回复
热议问题