I want to check if any element in noun matches any one of the elements in tables.
First element in nous in "devices". Clearly it matches with "projname:dataset.devices". If a match is found, loop should break, else it should check whether second element in noun, which is "today" matches any element in the tables.
tables = ["projname:dataset.devices","projname:dataset.attempts"]
noun = [devices,today]
I tried it with "noun in tables", i got empty result. Is there any other method that I can try with?
Thanks in advance !!
A simple use of any(n in s for n in nouns for s in tables) would suffice for a check.
If you actually want the matching item, you could write this quick function:
>>> def first_match(nouns, tables):
... for n in nouns:
... for t in tables:
... if n in t:
... return t
...
>>> first_match(nouns,tables)
'projname:dataset.devices'
Task:
Data:
tables = ["projname:dataset.devices","projname:dataset.attempts"]
noun = ['devices','today']
Generator expression:
This only gives the first match, as per OP request.
try:
print(next(t for n in noun for t in tables if n in t))
except StopIteration:
pass
Output:
'projname:dataset.devices'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With