Finding a tuple in a tuple

B

bg_ie

Hi,

Lists say I have the following tuple -

t1 = ("ONE","THREE","SIX")

and then the following tuples -

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.

Therefore -

2) ("ONE","TWO","THREE") : TRUE

3) ("TWO","FOUR","FIVE","SIX") : TRUE

4) ("TWO",) FALSE

5) ("TWO","FIVE")

How do I do this?

Cheers,

Barry.
 
P

Paul Rubin

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))
 
P

Paul McGuire

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
 
P

Paul Rubin

Paul McGuire said:
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.
 
P

Philipp Pagel

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.

Another way to go instead of using sets, although probably less elegant:
True in [x in t1 for x in t2] True
True in [x in t1 for x in t3] True
True in [x in t1 for x in t4] False
True in [x in t1 for x in t5]
False

cu
Philipp
 
J

Jussi Salmela

(e-mail address removed) kirjoitti:
Hi,

Lists say I have the following tuple -

t1 = ("ONE","THREE","SIX")

and then the following tuples -

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.

Therefore -

2) ("ONE","TWO","THREE") : TRUE

3) ("TWO","FOUR","FIVE","SIX") : TRUE

4) ("TWO",) FALSE

5) ("TWO","FIVE")

How do I do this?

Cheers,

Barry.

Another variation of the theme:

#====================
for t in (t2, t3, t4, t5):
for x in t1:
if x in t:
print True
break
else: print False
#====================


HTH,
Jussi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top