Search code examples
pythonif-statementgenerator-expression

get matched item from generator expression


I have written an if condition with a generator expression.

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

If condition is true, then I want to print that matching item name. How do I get this?


Solution

  • Don't use any(), and change your generator expression to using a filter (move the test to the end), then use next() to get the first match:

    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
    

    Or you could just use a for loop and break out after the first match:

    for item in self.keyword_list:
        if re.search(item.lower(), record['title'].lower() + ' ' + record['description'].lower()):
            print record
            break
    

    You could further clean any of these variants up by pre-computing the regular expression to match, and using the re.IGNORECASE flag so you don't have to lowercase everything:

    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
    

    or

    pattern = re.compile(
        '{} {}'.format(record['title'], record['description']),
        flags=re.IGNORECASE)
    for item in self.keyword_list:
        if pattern.search(item):
            print record
            break