简体   繁体   English

不区分大小写的字典值匹配

[英]Case-insensitive matching of dictionary values

Re .lower usage (?), how can I make case-insensitive matching for dictionary values? .lower用法(?),如何对字典值进行不区分大小写的匹配 For example, config[camID]["media_file"]] should match both, lowercase and uppercase extensions, even though the dictionary has lowercase only. 例如,即使字典仅具有小写字母, config[camID]["media_file"]]应匹配小写和大写扩展名。

1 line from dictionary: 字典中的1行:

config['d5']['media_file'] = ('nef', 'jpg', 'avi')

Code: 码:

for f in files:
     if any([f.endswith(x) for x in config[camID]["media_file"]]):
         os.rename(os.path.join(path, f), 
                   os.path.join(path, "%s%s%s" % (config[camID]['cam_name'], "_", f)))

(If there is an "in-dictionary" solution (ie, instead in the code), that would be also interesting to know.) (如果有一个“字典式”解决方案(例如,在代码中),那也很有趣。)

You need to convert the data to a standard before stores in the dict (like lower). 您需要先将数据转换为标准,然后再存储到dict中(如较低版本)。 After storage there is no way to search for case insensitive inside dict ou set, because this search are made by hash. 存储后,无法在字典集内部搜索不区分大小写的方法,因为此搜索是通过哈希进行的。

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

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