Finding a tuple in a tuple

Paul Rubin http
Thu Feb 22 05:27:21 EST 2007


"Paul McGuire" <ptmcg at austin.rr.com> writes:
> 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)

Well you do have to convert them to sets.  Also I thought each
intersection was wanted separately.  Otherwise if we're getting this
fancy, I guess I'd use (untested, uses new 2.5 "any" function):

  s1 = set(t1)
  print any((s1 & set(tn)) for tn in (t2,t3,t4,t5))

which avoids creating a potentially big intermediate set, and which
short-circuits (exits early) as soon as a match is found.



More information about the Python-list mailing list