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

2024-04-28 14:09:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我试图构建一个函数,在另一个列表的字符串中搜索列表的字符串,并返回所有匹配的单词。更具体地说,我试图检查我在两个列表(poa\u corporate\u identifier/poa\u cnpj\u 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')

Tags: oftheto字符串in列表anycorporate
2条回答

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

此外,您还需要检查term variances是否是下一个case的字符串,最后您不需要最后一个case,而是始终返回terms列表

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

将函数更改为下面的。你知道吗

def term_tracker(document, term_variations):
    if isinstance(term_variations, list):
        return [term for term in term_variations if len([i for i in document if term in i])]
    elif len([i for i in document if term_variations in i]):
        return term_variations
    else:
        print('No term located')

因为它没有返回列表,所以你只能得到一个值。你知道吗

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

相关问题 更多 >