Given a list of items, I want to create a function to check if it matches a certain criteria and return true or false.
Here is an example of the criteria and expected output:
#the to check against with criteria[1]
langList = ['spanish','english','russian','persian']
criteria = ['any'] #matches any language
result: match
#langList does not contain one or all
criteria = ['any-but-or',[english,chinese]]
result: no match
#langList does not contain all
criteria = ['any-but-and',[english,chinese]]
result: match
#langList contains one or all
criteria = ['any-with-or',[english,chinese]]
result: match
#langList contains all
criteria = ['any-with-and',[english,chinese]]
result: no match
#langList contains one or all and no others
criteria = ['only-with-or',[english,chinese]]
result: no match
#langList contains all and no others
criteria = ['only-with-and',[english,chinese]]
result: no match
What’s the best way to do this?
My idea is something like below but I’m haven’t mastered list comprehension yet which I think is key here.
def check_criteria_match(criteria, languageList):
rule = criteria[0]
criteriaLanguages = criteria[1]
match = True
if rule != 'any':
continue
elif rule = 'any-but-or' #check languageList:
match = False; break
#[...]
return match
EDIT:
Based on Praveen Gollakota’s answer, here is the final function, isn’t she pretty 🙂
def check_rule(rule, o, p):
# o = original, p = pattern
if rule == 'any':
return True
elif rule == 'any-but-or':
return not bool(set(p)-set(o))
elif rule == 'any-but-and':
return len(set(p)-set(o)) != 0
elif rule == 'any-with-or':
return len(set(p)-set(o)) <= 1
elif rule == 'any-with-and':
return len(set(p)-set(o)) == 0
elif rule == 'only-with-or':
return len(set(o)-set(p)) <= 1
elif rule == 'only-with-and':
return len(set(o)-set(p)) == 0
You want to do membership testing, for which
setsare the appropriate data structures andsetoperations work well.You can use sets and lambda functions to build a rule based dispatcher like this.
Of course, you need to add the rest of the rules in there.
EDIT: You can also write the rules inside a function like this.