从生成器表达式中获取匹配项
get matched item from generator expression
我用生成器表达式编写了一个 if
条件。
self.keyword_list = ['Buzz', 'Heard on the street', 'familiar with the development', 'familiar with the matter', 'Sources' ,'source', 'Anonymous', 'anonymity', 'Rumour', 'Scam', 'Fraud', 'In talks', 'Likely to', 'Cancel', 'May', 'Plans to', 'Raids' ,'raid', 'search', 'Delisting', 'delist', 'Block', 'Exit', 'Cheating', 'Scouts', 'scouting', 'Default', 'defaulted', 'defaulter', 'Calls off', 'Lease out', 'Pick up', 'delay', 'arrest', 'arrested', 'inks', 'in race', 'enters race', 'mull', 'consider', 'final stage', 'final deal', 'eye', 'eyes', 'probe', 'vie for', 'detects', 'allege', 'alleges', 'alleged', 'fabricated', 'inspection', 'inspected', 'to monetise', 'cancellation', 'control', 'pact', 'warning', 'IT scanner', 'Speculative', 'Divest', 'Buzz', 'Heard on the street', 'familiar with the development', 'familiar with the matter', 'Sources', 'source', 'Anonymous', 'anonymity', 'Rumour', 'Scam', 'Fraud', 'In talks', 'Likely to', 'Cancel', 'May', 'Plans to ', 'Raids', 'raid', 'search', 'Delisting', 'delist', 'Block', 'Exit', 'Cheating', 'Scouts','scouting', 'Default', 'defaulted', 'defaulter', 'Calls off', 'Lease out', 'Pick up', 'delay', 'arrest', 'arrested', 'inks', 'in race', 'enters race', 'mull', 'consider', 'final stage', 'final deal', 'eye', 'eyes', 'probe', 'vie for', 'detects', 'allege', 'alleges', 'alleged', 'fabricated', 'inspection', 'inspected', 'monetise', 'cancellation', 'control', 'pact', 'warning', 'IT scanner', 'Speculative', 'Divest']
if any(re.search(item.lower(), record['title'].lower()+' '+record['description'].lower()) for item in self.keyword_list):
#for which value of item condition became true?
#print item does not work
print record
如果条件为真,那么我想打印匹配的项目名称。我如何获得这个?
不要使用 any()
,并将生成器表达式更改为使用过滤器(将测试移至最后),然后使用 next()
获取第一个匹配项:
matches = (item for item in self.keyword_list if re.search(item.lower(), record['title'].lower() + ' ' + record['description'].lower()))
first_match = next(matches, None)
if first_match is not None:
print record
或者您可以只使用 for
循环并在第一场比赛后跳出:
for item in self.keyword_list:
if re.search(item.lower(), record['title'].lower() + ' ' + record['description'].lower()):
print record
break
您可以通过预先计算要匹配的正则表达式并使用 re.IGNORECASE
标志来进一步清理这些变体中的任何一个,这样您就不必将所有内容都小写:
pattern = re.compile(
'{} {}'.format(record['title'], record['description']),
flags=re.IGNORECASE)
matches = (item for item in self.keyword_list if pattern.search(item))
first_match = next(matches, None)
if first_match is not None:
print record
或
pattern = re.compile(
'{} {}'.format(record['title'], record['description']),
flags=re.IGNORECASE)
for item in self.keyword_list:
if pattern.search(item):
print record
break
我用生成器表达式编写了一个 if
条件。
self.keyword_list = ['Buzz', 'Heard on the street', 'familiar with the development', 'familiar with the matter', 'Sources' ,'source', 'Anonymous', 'anonymity', 'Rumour', 'Scam', 'Fraud', 'In talks', 'Likely to', 'Cancel', 'May', 'Plans to', 'Raids' ,'raid', 'search', 'Delisting', 'delist', 'Block', 'Exit', 'Cheating', 'Scouts', 'scouting', 'Default', 'defaulted', 'defaulter', 'Calls off', 'Lease out', 'Pick up', 'delay', 'arrest', 'arrested', 'inks', 'in race', 'enters race', 'mull', 'consider', 'final stage', 'final deal', 'eye', 'eyes', 'probe', 'vie for', 'detects', 'allege', 'alleges', 'alleged', 'fabricated', 'inspection', 'inspected', 'to monetise', 'cancellation', 'control', 'pact', 'warning', 'IT scanner', 'Speculative', 'Divest', 'Buzz', 'Heard on the street', 'familiar with the development', 'familiar with the matter', 'Sources', 'source', 'Anonymous', 'anonymity', 'Rumour', 'Scam', 'Fraud', 'In talks', 'Likely to', 'Cancel', 'May', 'Plans to ', 'Raids', 'raid', 'search', 'Delisting', 'delist', 'Block', 'Exit', 'Cheating', 'Scouts','scouting', 'Default', 'defaulted', 'defaulter', 'Calls off', 'Lease out', 'Pick up', 'delay', 'arrest', 'arrested', 'inks', 'in race', 'enters race', 'mull', 'consider', 'final stage', 'final deal', 'eye', 'eyes', 'probe', 'vie for', 'detects', 'allege', 'alleges', 'alleged', 'fabricated', 'inspection', 'inspected', 'monetise', 'cancellation', 'control', 'pact', 'warning', 'IT scanner', 'Speculative', 'Divest']
if any(re.search(item.lower(), record['title'].lower()+' '+record['description'].lower()) for item in self.keyword_list):
#for which value of item condition became true?
#print item does not work
print record
如果条件为真,那么我想打印匹配的项目名称。我如何获得这个?
不要使用 any()
,并将生成器表达式更改为使用过滤器(将测试移至最后),然后使用 next()
获取第一个匹配项:
matches = (item for item in self.keyword_list if re.search(item.lower(), record['title'].lower() + ' ' + record['description'].lower()))
first_match = next(matches, None)
if first_match is not None:
print record
或者您可以只使用 for
循环并在第一场比赛后跳出:
for item in self.keyword_list:
if re.search(item.lower(), record['title'].lower() + ' ' + record['description'].lower()):
print record
break
您可以通过预先计算要匹配的正则表达式并使用 re.IGNORECASE
标志来进一步清理这些变体中的任何一个,这样您就不必将所有内容都小写:
pattern = re.compile(
'{} {}'.format(record['title'], record['description']),
flags=re.IGNORECASE)
matches = (item for item in self.keyword_list if pattern.search(item))
first_match = next(matches, None)
if first_match is not None:
print record
或
pattern = re.compile(
'{} {}'.format(record['title'], record['description']),
flags=re.IGNORECASE)
for item in self.keyword_list:
if pattern.search(item):
print record
break