0

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

如果条件为真,那么我想打印匹配的项目名称。我怎么得到这个?

4

1 回答 1

1

不要使用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
于 2015-09-10T09:36:26.507 回答