简体   繁体   English

寻找关键词

[英]Finding key word

res = dict(filter(lambda item: search_key in item[0], dict1.items()))
list_keys = [ k for k in res ]

Here is my code.这是我的代码。 I want it to only include the list of words, not any additional concatenation with the exception of - ,"cats" like I am searching cat I want the list not to return catspoon .我希望它只包含单词列表,而不是任何额外的连接,除了- “cats” 就像我正在搜索cat我希望列表不返回catspoon

Use a regular expression to match whole words.使用正则表达式匹配整个单词。

import re

search_key = r'\bcat\b'
res = dict(filter(lambda item: re.search(search_key, item[0]), dict1.items()))
list_keys = [ k for k in res ]

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

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