Finding a tuple in a tuple

Paul McGuire ptmcg at austin.rr.com
Thu Feb 22 04:55:43 EST 2007


On Feb 22, 3:05 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> b... at yahoo.com writes:
> > Lists say I have the following tuple -
> > t1 = ("ONE","THREE","SIX")
> > t2 = ("ONE","TWO","THREE")
> > t3 = ("TWO","FOUR","FIVE","SIX")
> > t4 = ("TWO",)
> > t5 = ("TWO","FIVE")
>
> > What I want to do is return true if any member of tuple t1 is found in
> > the remaining tuples.
>
> Convert them into sets and use the set intersection (&) operator,
> then convert to bool to represent whether the intersection is empty.
>
>     print bool(set(t1) & set(t2))
>     print bool(set(t1) & set(t3))
>     print bool(set(t1) & set(t4))
>     print bool(set(t1) & set(t5))

A step further: use union to make a superset of t2-tN, then use & on
this superset.

setlist = [t2,t3,t4,t5]
superset = reduce(set.union, map(set,setlist) )
print bool(t1 & superset)


-- Paul




More information about the Python-list mailing list