首页 > 解决方案 > 如何检查字符串列表是否存在任何元组

问题描述

我正在尝试检查另一个字符串列表中存在多少元组列表中的元组,并且我希望所有元组也与特定字符串相关联。

例子:

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)

这种方法效果很好,可以更优化吗?

标签: pythonpandasnumpy

解决方案


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']

推荐阅读