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. 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))
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
"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. 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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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'
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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,...
|
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...
|
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...
| |