Python 3.x python字典子字符串键

Python 3.x python字典子字符串键,python-3.x,string,dictionary,Python 3.x,String,Dictionary,我有一本这样的字典 D={'Physician procedure note':a,'Dentistry Procedure note':b,'Podiatry Procedure note':c} 我想用上面字典中的“过程报告”键搜索这本字典。我想找出字典中最接近的键并提取值 I have used this approach search_key = 'Procedure report' # Using items() + list comprehension # Substri

我有一本这样的字典

D={'Physician procedure note':a,'Dentistry Procedure note':b,'Podiatry Procedure note':c}
我想用上面字典中的“过程报告”键搜索这本字典。我想找出字典中最接近的键并提取值

I have used this approach
search_key = 'Procedure report'
  

# Using items() + list comprehension 
# Substring Key match in dictionary 
result = [val for key, val in D.items() if search_key in key] 
  
# printing result  
print("Values for substring keys : " + str(result)) 
我得到一张空名单。如何更改此设置。

您可以试试

result = [val for key, val in D.items() if any([s.lower() in key.lower() for s in search_key.split(" ")])]
print(result)
输出

['a', 'b', 'c']

也可以通过选中“设置交点”来执行此操作:

search_key = set('Procedure report'.lower().split(" "))

print([v for k, v in D.items() if search_key.intersection(set(k.lower().split(" ")))])
# ['a', 'b', 'c']

假设您想要检索一个值列表,其中对应的键与搜索键(部分)匹配,那么如果搜索键不匹配,则
将无法工作,因为这需要完全匹配

你能做的一件事就是

#构造一组搜索关键字
关键字=set(search_key.upper().split(“”)
D_filter=list(filter(lambda x:keywords.intersection)(set(x[0].upper().split(“”)),D.items())
结果=[D_过滤器中的项的项[1]

预期输出是什么?如果我将“过程报告”作为匹配的键。我想检索上面字典“医生程序注释”、“牙科程序注释”、“足部治疗程序注释”中的所有键。
res
字典是什么?您的
D
词典不包含您的搜索关键字两者都是相同的。dict中的一个关键字包含文本
过程报告