不区分大小写的词典搜索? [英] Case insensitive dictionary search?
问题描述
我可以使用map使用Python来实现不区分大小写的列表搜索.
a = ['xyz', 'wMa', 'Pma'];
b = map(string.lower, a)
if 'Xyz'.lower() in b:
print 'yes'
如何用字典做同样的事情?
我尝试了以下代码,但是ap具有['a','b','c']的列表,而不区分大小写的字典.
a = {'a':1, 'B':2, 'c':3}
ap = map(string.lower, a)
请注意,使字典不区分大小写,无论采用何种方式,都可能会丢失信息:例如,您将如何不区分大小写" {'a': 23, 'A': 45}
? !如果您只关心键中是否存在键(即,不在乎与之对应的值),则改为使用set
,即
theset = set(k.lower() for k in thedict)
(在每个Python版本中,或{k.lower() for k in thedict}
,如果您对只在Python 2.7或更高版本中运行的代码感到满意,则出于某种纯粹的装饰性语法糖;-),并使用if k.lower() in theset: ...
进行检查. /p>
或者,您可以创建一个包装器类,例如,一个只读类...:
import collections
class CaseInsensitiveDict(collections.Mapping):
def __init__(self, d):
self._d = d
self._s = dict((k.lower(), k) for k in d)
def __contains__(self, k):
return k.lower() in self._s
def __len__(self):
return len(self._s)
def __iter__(self):
return iter(self._s)
def __getitem__(self, k):
return self._d[self._s[k.lower()]]
def actual_key_case(self, k):
return self._s.get(k.lower())
这将保留(实际上并不需要更改原始词典,因此,如果需要的话,仍然可以为它检索所有精确的信息)键的可能多个值中的任意一个,这些键由于以下原因折叠"为单个键:不区分大小写,并提供字典的所有只读方法(仅带字符串键)以及actual_key_case
方法,该方法返回用于任何给定字符串键的实际大小写混合(如果没有大小写更改,则返回None
给定的字符串键匹配字典中的任何键.
I can use map to implement the case insensitive list search with Python.
a = ['xyz', 'wMa', 'Pma'];
b = map(string.lower, a)
if 'Xyz'.lower() in b:
print 'yes'
How can I do the same thing with dictionary?
I tried the following code, but ap has the list of ['a','b','c'], not the case insensitive dictionary.
a = {'a':1, 'B':2, 'c':3}
ap = map(string.lower, a)
Note that making a dictionary case-insensitive, by whatever mean, may well lose information: for example, how would you "case-insensitivize" {'a': 23, 'A': 45}
?! If all you care is where a key is in the dict or not (i.e., don't care about what value corresponds to it), then make a set
instead -- i.e.
theset = set(k.lower() for k in thedict)
(in every version of Python, or {k.lower() for k in thedict}
if you're happy with your code working only in Python 2.7 or later for the sake of some purely decorative syntax sugar;-), and check with if k.lower() in theset: ...
.
Or, you could make a wrapper class, e.g., maybe a read-only one...:
import collections
class CaseInsensitiveDict(collections.Mapping):
def __init__(self, d):
self._d = d
self._s = dict((k.lower(), k) for k in d)
def __contains__(self, k):
return k.lower() in self._s
def __len__(self):
return len(self._s)
def __iter__(self):
return iter(self._s)
def __getitem__(self, k):
return self._d[self._s[k.lower()]]
def actual_key_case(self, k):
return self._s.get(k.lower())
This will keep (without actually altering the original dictionary, so all precise information can still be retrieve for it, if and when needed) an arbitrary one of possibly-multiple values for keys that "collapse" into a single key due to the case-insensitiveness, and offer all read-only methods of dictionaries (with string keys, only) plus an actual_key_case
method returning the actual case mix used for any given string key (or None
if no case-alteration of that given string key matches any key in the dictionary).
这篇关于不区分大小写的词典搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!