473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Finding a tuple in a tuple

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.

Feb 22 '07 #1
6 1208
bg***@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))
Feb 22 '07 #2
On Feb 22, 3:05 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
b...@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

Feb 22 '07 #3
"Paul McGuire" <pt***@austin.rr.comwrites:
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.
Feb 22 '07 #4
bg***@yahoo.com wrote:
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

--
Dr. Philipp Pagel Tel. +49-8161-71 2131
Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186
Technical University of Munich
http://mips.gsf.de/staff/pagel
Feb 22 '07 #5
bg***@yahoo.com 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
Feb 22 '07 #6
Philipp Pagel <pD*******@gsf.dewrote:
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
Slightly more elegant for Python 2.5 users:
>>any(x in t1 for x in t2)
True
>>any(x in t1 for x in t3)
True
>>any(x in t1 for x in t4)
False
>>any(x in t1 for x in t5)
False
Feb 23 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
7
by: Ben | last post by:
Hi In a list I have a number of soccer players. Each player has a different rating for attacking, defending, midfield fitness and goalkeeping. I have devised a while loop that goes through...
3
by: Linh Luong | last post by:
Hi All, 1. I have been reading and the max size of a tuple is 8K. I have also read that I can it to a larger size in some config file. Where is this file? is it called pg_config.h and is the...
1
by: Simon Forman | last post by:
I've got a function that I'd like to improve. It takes a list of lists and a "target" element, and it returns the set of the items in the lists that appear either before or after the target...
1
by: t.mitchell | last post by:
Hi, I have a python gtk app that allows users to have one project open at a time. I have recently discovered that projects are not being freed when they are closed - the refcount is not hitting...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
7
by: Breal | last post by:
I have a list that looks like the following I would like to be able to determine which of these overlap each other. So, in this case, tuple 1 overlaps with tuples 2 and 3. Tuple 2 overlaps...
4
by: Alex Vinokur | last post by:
Here is some tuple (triple in this case) with uniform types (for instance, double): boost::tuple<double, double, doublet; Is there any way (in boost::tuple) to define such tuples something like...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.