I'm using Python 3.4.2 at the moment but am not getting the expected results when attempting to search for a number of strings within another string.
I create a set which contains items with a string format similar to TEXT.NA[Y]ABC. I'm trying to only catch items of the set which contain a .NA, .SA or .EU as an example.
testset = set()
testset.add(('Blah','TEXT.NA[Y]ABC'))
testset.add(('Bleh','OTHER.AU[X]DEF'))
region = ['.NA', '.SA', '.EU']
for text,key in testset:
if any(sym in region for sym in key):
print(key)
I was expecting the above to print TEXT.NA[Y]ABC while skipping OTHER.AU[X]DEF Wondering what I'm doing wrong in my iterable.
Thanks!
Your membership checking is wrong. You need to check if any item from region
is in key
:
>>> for text,key in testset:
... if any(sym in key for sym in region):
... print(key)
...
TEXT.NA[Y]ABC