[英]What's the fastest way to identify the 'name' of a dictionary that contains a specific key-value pair?
I'd like to identify the dictionary within the following list that contains the key-value pair 'Keya':'123a', which is ID1 in this case. 我想在下面的列表中标识包含键值对'Keya':'123a'的字典,在这种情况下为ID1。
lst = {ID1:{'Keya':'123a','Keyb':456,'Keyc':789},ID2:{'Keya':'132a','Keyb':654,'Keyc':987},ID3:{'Keya':'5433a','Keyb':222,'Keyc':333},ID4:{'Keya':'444a','Keyb':777,'Keyc':666}}
It's safe to assume all dictionaries have the same key's, but have different values. 可以安全地假设所有字典都具有相同的键,但是具有不同的值。
I currently have the following to identify which dictionary has the value '123a' for the key 'Keya', but is there a shorter and faster way? 我目前有以下内容来确定哪个字典的键“ Keya”的值为“ 123a”,但是有没有更短,更快的方法?
DictionaryNames = map(lambda Dict: str(Dict),lst)
Dictionaries = [i[1] for i in lst.items()]
Dictionaries = map(lambda Dict: str(Dict),Dictionaries)
Dict = filter(lambda item:'123a' in item,Dictionaries)
val = DictionaryNames[Dictionaries.index(Dict[0])]
return val
If you actually had a list of dictionaries, this would be: 如果您实际上有一个词典列表,则为:
next(d for d in list_o_dicts if d[key]==value)
Since you actually have a dictionary of dictionaries, and you want the key associated with the dictionary, it's: 由于您实际上有一个词典字典,并且想要与该词典相关联的键,因此它是:
next(k for k, d in dict_o_dicts.items() if d[key]==value)
This returns the first matching value. 这将返回第一个匹配值。 If you're absolutely sure there is exactly one, or if you don't care which you get if there are more than one, and if you're happy with a
StopIteration
exception if you were wrong and there isn't one, that's exactly what you want. 如果您完全确定确实有一个,或者如果您不关心是否有多个,那么您会从中得到,如果您对
StopIteration
异常感到满意而又没有一个,那么您就StopIteration
感到满意,那就是正是您想要的。
If you need all matching values, just do the same with a list comprehension: 如果需要所有匹配的值,只需对列表进行理解即可:
[k for k, d in dict_o_dicts.items() if d[key]==value]
That list can of course have 0, 1, or 17 values. 该列表当然可以具有0、1或17个值。
You can just do [name for name, d in lst.iteritems() if d['Keya']=='123a']
to get a list of all the dictionaries in lst
that have that value for that key. 您只需
[name for name, d in lst.iteritems() if d['Keya']=='123a']
执行[name for name, d in lst.iteritems() if d['Keya']=='123a']
以获取lst
中所有具有该键值的所有字典的列表。 If you know there is only one, you can get it with [name for name, d in lst.iteritems() if d['Keya']=='123a'][0]
. 如果您知道只有一个,则可以使用
[name for name, d in lst.iteritems() if d['Keya']=='123a'][0]
。 (As Andy mentions in a comment, your name lst
is misleading, since lst
is actually a dictionary of dictionaries, not a list.) (正如安迪在评论中提到的那样,您的名字
lst
具有误导性,因为lst
实际上是字典的字典,而不是列表。)
Å different way to do this is to use the appropriate data structure: Keep a "reverse map" of key-value pairs to names. 另一种执行此操作的方法是使用适当的数据结构:保留键值对与名称的“反向映射”。 If your dictionary of dictionaries is static after being built, you can build the reverse dictionary like this:
如果您的字典词典在构建后是静态的,则可以按以下方式构建反向词典:
revdict = {(key, value): name
for name, subdict in dictodicts.items()
for key, value in subdict.items()}
If not, you just need to add revdict[key, value] = name
for each d[name][key] = value
statement and build them up in parallel. 如果不是,则只需为每个
d[name][key] = value
语句添加revdict[key, value] = name
并并行构建它们。
Either way, to find the name of the dict that maps key
to value
, it's just: 无论哪种方式,要找到将
key
映射到value
的dict的名称,就是:
revdict[key, value]
For (a whole lot) more information (than you actually want), and some sample code for wrapping things up in different ways… I dug up an unfinished blog post, considered editing it, and decided to not bother and just clicked Publish instead, so: Reverse dictionary lookup and more, on beyond z . 对于(大量)更多信息(比您实际想要的信息)以及一些示例代码,它们以不同的方式包装它们……我挖出了一篇未完成的博客文章,考虑对其进行编辑,并决定不打扰,只是单击了Publish,所以: 反向字典查找等等,除了z之外 。
Since you want the fastest, you should short-cut your search as soon as you find the data you are after. 由于需要最快的速度,因此,一旦找到所需的数据,就应该立即缩短搜索速度。 Iterating through the whole list is not necessary, nor is producing any temporary dictionary:
不需要遍历整个列表,也不需要生成任何临时字典:
for key,data in lst.iteritems():
if data['Keya']=='132a':
return key #or break is not in a function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.