我是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')
答案 0 :(得分:0)
您将通过return term
返回匹配的第一个术语,而您需要将在文档列表中匹配的所有术语附加到术语列表中,然后返回该列表
另外,您还需要检查术语变体是否为下一种情况的字符串,最后您不需要最后一种,则始终返回术语列表
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']
答案 1 :(得分:0)
将功能更改为下方。
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']