python - 返回在不同列表中匹配的字符串的函数

标签 python

我是 Python 新手,所以如果我的问题看起来很愚蠢,请提前道歉。

我正在尝试构建一个函数,该函数在另一个列表的字符串中搜索列表的字符串并返回所有匹配的单词。更具体地说,我试图检查我在 2 个列表(poa_corporate_identifier/poa_cnpj_identifier)中编译的某些关键字是否位于下面的文本列表中。

出于某种原因,当我知道关键字列表中还有更多项目也位于文本列表的某些字符串中时,我不断收到单个字符串作为输出。

任何人都可以帮助我确定为什么我的代码没有给出预期的结果(或建议另一种有效的方法来实现我的目标)?

提前致谢!

text = ['power of attorney',
'(b) to attend any partners’ meeting; (c) to represent the grantor 
regarding any change or amendment to the articles of association; (c) to 
receive service of process on behalf of the grantor in the event of 
judicial proceedings arising from applicable corporate laws in brazil; (d) 
to represent the grantor before the central bank of brazil; (e) to 
represent the grantor before the brazilian federal revenue office; (f) to 
deal and solve any issues before the brazilian federal revenue office, and 
to sign any document before that agency including, but not limited to, the 
basic cnpj entry document',
'in witness whereof, grantor has caused this document to be executed by 
its chief executive officer, mr. [?], in the city of [•], on this [•] day 
of [•], [•].']

poa_corporate_identifier = ['articles of association', "partners' 
meeting", "shareholders meeting", 'corporate laws', 'corporate books', 
'board of commerce']
poa_cnpj_identifier = ['brazilian federal revenue office', 'cnpj', 'basic 
cnpj entry document']
poa_nature = poa_corporate_identifier + poa_cnpj_identifier

def term_tracker(document, term_variations):
    if isinstance(term_variations, list) == True:
        for term in term_variations:
            if any([str(term) in i for i in document]) == True:
                return term              
    if any([term_variations in i for i in document]) == True:
        return term_variations
    else:
        print('No term located')

最佳答案

您将通过返回术语返回匹配的第一个术语,而是需要将文档列表中匹配的所有术语附加到术语列表中,然后返回该列表

此外,您还需要检查术语变体是否是下一种情况的字符串,最后您不需要最后一个 else,您始终返回术语列表

def term_tracker(document, term_variations):

    terms = []
    #If term variations is a list
    if isinstance(term_variations, list) == True:
        for term in term_variations:
            #If we find a term in the document, append that term to a list
            if any([str(term) in i for i in document]):
                terms.append(term)

    #If it is a string, find that string in all documents
    elif isinstance(term_variations, str) == True:
        if any([term_variations in i for i in document]) == True:
            terms.append(term_variations)

    return terms

print(term_tracker(text, poa_nature))
print(term_tracker(text, 'cnpj'))

输出将是

['articles of association', 'corporate laws', 'brazilian federal revenue office', 'cnpj', 'basic cnpj entry document']
['cnpj']

关于python - 返回在不同列表中匹配的字符串的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56039063/

相关文章:

python - 用于降维的 Scikit-learn 主成分分析 (PCA)

Python 2 最大功能

python - 使用 Python 范围和日历检查闰年

python - 在 Python 语言中从多个线程访问变量

python - 将一年与数据值分开;照顾闰年

python - 如何通过 ssh key 使用 Fabric?

python - 在 wxPython Canvas 中强制重绘

python - 如何在 Django 模板中使用 break 和 continue?

python - 跟踪 python 源文件中的更改?

python - 将字典中的字符串值转换为 int