Reputation: 286
Suppose I have this set:
a = set([(1,2),(3,4),(5,6)])
How do I test if "3" or "7" can be found in that set?
Upvotes: 0
Views: 119
Reputation: 212835
a = set([(1,2),(3,4),(5,6)])
b = set((3,7))
any(b&set(p) for p in a)
# True
@RikPoggi also proposed using isdisjoint
, which works even without creating a set:
any(not b.isdisjoint(p) for p in a)
Upvotes: 2
Reputation: 17173
>>> from itertools import chain
>>> if set(chain(*set([(1,2),(3,4),(5,6)])))&set([3,7]):
... print True
...
True
Upvotes: 0
Reputation: 2264
for tuple in a:
for value in tuple:
if value in [3, 7]:
print 'found'
Upvotes: 1