any()
creates True
or False
, and generator expression variables are not available outside the expression:
>>> l = ['S', 'R', 'D', 'W', 'V', 'Y', 'H', 'K', 'B', 'M']
>>> s = 'YTG'
>>> any(i in s for i in l)
True
>>> i
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined
Use the list to list all the matching letters:
matching = [i for i in l if i in s]
if matching:
print matching
This keeps order in l
; if the order in matching
doesn't matter, you can just use the given intersections. I would make a l
set here:
l = set(['S', 'R', 'D', 'W', 'V', 'Y', 'H', 'K', 'B', 'M'])
matching = l.intersection(s)
if matching:
print matching
l
, Python s
.