[Python Edinburgh] Comparing two Tuples
Lars Wirzenius
liw at liw.fi
Wed Oct 20 16:14:25 CEST 2010
On ke, 2010-10-20 at 15:03 +0100, Dougal Matthews wrote:
> What is the best to compare two tuples and see if they contain one or
> more of the same items.
>
> A = (1,2,3)
> B = (3,4,5)
> C = (6,7,8)
>
> Where A would match B because they both have 3 and C wouldn't match
> either.
Sets would be a simple solution for this.
>>> a=(1,2,3)
>>> b=(3,4,5)
>>> c=(6,7,8)
>>> set(a).intersection(set(b))
set([3])
>>> set(a).intersection(set(c))
set([])
>>> if set(a).intersection(set(c)): print 'yay'
...
>>> if set(a).intersection(set(b)): print 'yay'
...
yay
>>>
More information about the Edinburgh
mailing list