regex - 基于多个列表中存在的单词的关键字检查
问题描述
我有一本类似的字典:
countries = ["usa", "france", "japan", "china", "germany"]
fruits = ["mango", "apple", "passion-fruit", "durion", "bananna"]
cf_dict = {k:v for k,v in zip(["countries", "fruits"], [countries, fruits])}
我也有一个类似的字符串列表:
docs = ["mango is a fruit that is very different from Apple","I like to travel, last year I was in Germany but I like France.it was lovely"]
我想检查docs
并查看每个字符串是否包含any
cf_dict 中任何列表(cf_dict 的值是列表)中的关键字,如果它们存在则返回该字符串的对应key
(基于值)(字符串在文档中)作为输出。
例如,如果我检查列表docs
,输出将是 [ fruits
, countries
]
类似于此答案的内容,但这仅检查一个列表,但是,我想检查多个列表。
解决方案
如果字符串与多个列表中的值匹配(例如'apple grows in USA'
应该映射到{'fruits', 'countries'}
),则以下返回集合的字典。
print({s: {k for k, l in cf_dict.items() for w in l if w in s.lower()} for s in docs})
这输出:
{'mango is a fruit that is very different from Apple': {'fruits'}, 'I like to travel, last year I was in Germany but I like France.it was lovely': {'countries'}}
推荐阅读
- python - 在 opencv-python cap.read() 1 秒后返回 false
- sql - 将数据类型从 varchar 更改为 integer
- javascript - 当对象中的字段发生更改时如何从 v-model 数组中删除对象
- mysql - 它有什么错误,很简单,错误显示在最后一个“结束”语句中。什么是错误
- java - LWJGL+XCB 随机崩溃
- android - recyclerView.scheduleLayoutAnimation() 是如何工作的?
- c# - 数据未从 Office 365 的 Outlook 中检索?
- php - 如何将二进制转换为base64?
- java - 签名的 jar 被 java 安全阻止运行
- rust - 加号的 Rust 宏重复