David542
David542

Reputation: 110592

One-liner for partial string match in list lookup

Is there a simpler way to do the following?

filename = 'vudu_hail_20140101.xml'
acceptable_stems = ['vudu', 'google']

process_file = False
for acceptable_stem in acceptable_stems:
    if acceptable_stem in filename:
        process_file = True

Basically, I'm looking for a Boolean determinant on whether one of the stems is in the filename. How would this be done with a one-liner?

Upvotes: 2

Views: 51

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76391

How about using the any keyword:

any([acceptable_stem in filename for acceptable_stem in acceptable_stems])

Examples:

>> filename = 'vudu_hail_20140101.xml'
>> acceptable_stems = ['vudu', 'google']
>> any([acceptable_stem in filename for acceptable_stem in acceptable_stems])
True

>> filename = 'vudu_hail_20140101.xml'
>> acceptable_stems = ['vuduf', 'google']
>> any([acceptable_stem in filename for acceptable_stem in acceptable_stems])
False

Upvotes: 2

Related Questions