python - 如何检查字符串列表是否存在任何元组
问题描述
我正在尝试检查另一个字符串列表中存在多少元组列表中的元组,并且我希望所有元组也与特定字符串相关联。
例子:
A = [["is", "a"],["is", "going"],["to", "the"]]
B = ["This is a bat", "Adam is going to the school"]
我想要哪个元组存在于哪个字符串中的结果。 期望的输出
["is", "a"] exists in "This is a bat"
["is", "going"] and ["to", "the"] exists in "Adam is going to the school"
我已经尝试了下面的代码,但它只给出了列表 B 中存在的所有元组
A = [["is", "a"],["is", "going"],["to", "the"]]
B = ["This is a bat", "Adam is going to the school"]
matching = [s for s in B if any(x in s for x in A)]
编辑: 尝试了另一种方法
A = [["is", "a"],["is", "going"],["to", "the"]]
B = ["This is a bat", "Adam is going to the school"]
for i in range(len(B)):
flag = False
keywords = ""
for a in A:
if a[0]+" "+a[1] in B[i]:
if(keywords == ""):
keywords = a[0]+" "+a[1]
else:
keywords = keywords + ", " + a[0]+" "+a[1]
print(keywords)
这种方法效果很好,可以更优化吗?
解决方案
import re
A = [["is", "a"], ["is", "going"], ["to", "the"]]
joined_A = [" ".join(a) for a in A]
B = ["This is...a bat!", "Adam is going to the school"]
for s in B:
print(s)
normalised = " ".join(re.findall(r"\b\w+\b", s))
for a, joined in zip(A, joined_A):
if joined in normalised:
print(a)
输出:
This is...a bat!
['is', 'a']
Adam is going to the school
['is', 'going']
['to', 'the']
推荐阅读
- swiftui - SwiftUI NavigationLink 自动弹回
- google-admin-sdk - 管理员目录 V1 列表角色
- reactjs - 不确认 Edit on Press ENTER with AG GRID REACT
- php - 为什么我不能在数据库中插入这个查询
- laravel - 集成或单元测试
- class - 继承类看不到基类
- firebase-realtime-database - 在时间间隔内从 Firebase 中删除数据库
- qt - 在 Linux 机器上将 Qt 部署到 Windows
- open-policy-agent - 如何在 opa 服务器上使用简单的 hello world 示例
- android - 安卓。ImageView 保持全屏图像的纵横比