[英]dictionary of dictionaries, get keys in common for values
I have a very big dictionary of dictionaries and it is like this: 我有一本非常大的词典字典,它是这样的:
Dict={,
...
'25465466':{'Cmstrk': 'cms_trk_dcs_05:CAEN', 'Crate': 'easyCrate0', 'Board': 'easyBoard06', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_4', 'Channel': 'channel003\n'},
'436232302': {'Cmstrk': 'cms_trk_dcs_03:CAEN', 'Crate': 'easyCrate1', 'Board': 'easyBoard01', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_8', 'Channel': 'channel002\n'},
'470311412': {'Cmstrk': 'cms_trk_dcs_03:CAEN', 'Crate': 'easyCrate0', 'Board': 'easyBoard00', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_4', 'Channel': 'channel003\n'},
...
}
And if the user types cms_trk_dcs_05:CAEN
or easyCrate1/easyBoard01
or more combination of this values the script has to return those keys (numbers like '654546536') that they have in common. 并且,如果用户键入
cms_trk_dcs_05:CAEN
或easyCrate1/easyBoard01
或此值的更多组合,则脚本必须返回它们共同拥有的那些键(如“ 654546536”之类的数字)。
For example if input is easyCrate0/CMS_TRACKER_SY1527_4
the answer is 470311412,25465466
. 例如,如果输入为
easyCrate0/CMS_TRACKER_SY1527_4
则答案为470311412,25465466
。
I guess this should help. 我想这应该有所帮助。
Dict = {
'25465466':{'Cmstrk': 'cms_trk_dcs_05:CAEN', 'Crate': 'easyCrate0', 'Board': 'easyBoard06', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_4', 'Channel': 'channel003\n'},
'436232302': {'Cmstrk': 'cms_trk_dcs_03:CAEN', 'Crate': 'easyCrate1', 'Board': 'easyBoard01', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_8', 'Channel': 'channel002\n'},
'470311412': {'Cmstrk': 'cms_trk_dcs_03:CAEN', 'Crate': 'easyCrate0', 'Board': 'easyBoard00', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_4', 'Channel': 'channel003\n'}
}
def sub_search(terms):
check = lambda k: all(map(lambda term:term in Dict[k].values(), terms))
return [key for key,value in Dict.items() if check(key)]
def search(term):
terms = term.split('/')
return sub_search(terms)
search('cms_trk_dcs_05:CAEN')
#['25465466']
search('easyCrate1/easyBoard01')
#['436232302']
inp = "easyCrate0/CMS_TRACKER_SY1527_4".split("/")
common = []
for k in Dict:
if all(x in Dict[k].values() for x in inp): # if all of the values match the user input, add it to common list
common.append(k)
print (common)
['25465466', '470311412']
You could also use sets: 您还可以使用集合:
common = []
for k in Dict:
if len(set(inp).intersection(Dict[k].values())) == len(inp):
common.append(k)
You can use a list comp also: 您还可以使用列表组合:
common = [k for k in Dict if len(set(inp).intersection(Dict[k].values())) == len(inp)]
FINAL EDIT: Here is the code cleaner: 最终编辑:这是代码清除器:
from collections import OrderedDict
find = raw_input("What are you looking for? ")
finder = find.split('/')
similar = []
for key in Dict:
for x in range(0, len(finder)):
for pairs in Dict[key].items():
if pairs[1] == finder[0]:
similar.append(key)
if len(similar) == 1 or len(finder) == 1:
print ",".join(similar)
for items in similar:
similar.remove(items)
print ",".join(list(OrderedDict.fromkeys(similar)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.