473,396 Members | 2,158 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,396 software developers and data experts.

Proposal: s1.intersects(s2)


Here's an implementation of the functionality I propose, as a
free-standing function:

def intersects(s1,s2):
if len(s1) < len(s2):
for x in s1:
if x in s2: return True
else:
for x in s2:
if x in s1 return True
return False

Right now, the only convenient thing to do is

if s1 & s2 ...

but that builds a whole new set. IMO that query should be available
as a method of set itself.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==http://www.astoriaseminar.com

Jul 4 '07 #1
14 1612
On Wed, 04 Jul 2007 09:59:24 -0400, David Abrahams wrote:
Here's an implementation of the functionality I propose, as a
free-standing function:

def intersects(s1,s2):
if len(s1) < len(s2):
for x in s1:
if x in s2: return True
else:
for x in s2:
if x in s1 return True
return False
In Python 2.5 this can be written a bit more concise:

def intersects(set_a, set_b):
if len(set_a) < len(set_b):
set_a, set_b = set_b, set_a
return any(item in set_a for item in set_b)

Ciao,
Marc 'BlackJack' Rintsch
Jul 4 '07 #2
On Wed, 04 Jul 2007 14:37:34 +0000, Marc 'BlackJack' Rintsch wrote:
On Wed, 04 Jul 2007 09:59:24 -0400, David Abrahams wrote:
>Here's an implementation of the functionality I propose, as a
free-standing function:

def intersects(s1,s2):
if len(s1) < len(s2):
for x in s1:
if x in s2: return True
else:
for x in s2:
if x in s1 return True
return False

In Python 2.5 this can be written a bit more concise:

def intersects(set_a, set_b):
if len(set_a) < len(set_b):
set_a, set_b = set_b, set_a
return any(item in set_a for item in set_b)

I'd rather see sets gain an method "isintersect()" with the functionality
than the language to gain a built-in function.

However, there's a very subtle flaw in the idea. While "the intersection"
of two sets is well-defined, "these two sets intersect" is (surprisingly!)
_not_ well-defined. If you only consider non-empty sets, you won't have a
problem: two non-empty sets intersect if their intersection is not empty,
and both implementations of intersects() given will do the right thing.

The problem comes if we (perhaps naively) try to say that if a set A is a
subset of set B, set A must intersect with B. (Not all intersecting sets
are subsets, but all subsets are intersecting sets.) Unfortunately that is
not the same as asking if the intersection between two sets is not empty:
>>A = set()
B = set([12, 21])
A.issubset(B)
True
>>B.issuperset(A)
True
>>intersects(A, B) # both implementations do the same
False
>>intersects(A, A) # does A intersect with itself?
False
PLEASE don't anybody try to argue that Python's behaviour here is "a bug"
-- the meaning of subset and superset with the empty set is
well-established and completely uncontroversial amongst mathematicians.
(It is what they call a vacuous truth.)

Rather than discuss the issues in detail, I'll just point those who care
to Wikipedia:

http://en.wikipedia.org/wiki/Empty_set#Common_problems

and point out that the behaviour of any() and all() can sometimes be
unintuitive or contradictory for the same reason.

As a result, any proposed function or method that returns a True/False
value for whether set A intersects with set B needs to define (and
justify) what it means to say that two sets intersect when one or both are
the empty set.

--
Steven.

Jul 5 '07 #3
Steven D'Aprano wrote:
The problem comes if we (perhaps naively) try to say that if a set A is a
subset of set B, set A must intersect with B. (Not all intersecting sets
are subsets, but all subsets are intersecting sets.) Unfortunately that is
not the same as asking if the intersection between two sets is not empty:
Well, sure. If you're dealing with empty sets, then you should know
what you're getting yourself into if you're asking about set
intersections. If you don't, then an .intersects method isn't going to
help you out of your conundrum of not understanding how the null set
relates to intersections.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Perfect girl / She was never me
-- Lamya
Jul 5 '07 #4
On Jul 4, 8:14 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
However, there's a very subtle flaw in the idea. While "the intersection"
of two sets is well-defined, "these two sets intersect" is (surprisingly!)
_not_ well-defined.
Poppycock! It's perfectly well defined: two sets intersect if and
only if their intersection is nonempty. There's absolutely no reason
to single out the empty set for special treatment in this definition.
The problem comes if we (perhaps naively) try to say that if a set A is a
subset of set B, set A must intersect with B.
Well of course false statements are going to cause problems.
(Not all intersecting sets are subsets, but all subsets are intersecting sets.)
Not true.
As a result, any proposed function or method that returns a True/False
value for whether set A intersects with set B needs to define (and
justify) what it means to say that two sets intersect when one or both are
the empty set.
Nope. There's one, obvious, correct definition, as given above. No
need to mention the empty set at all.

Richard

Jul 5 '07 #5

on Wed Jul 04 2007, "Steven D'Aprano" <steve-AT-REMOVE.THIS.cybersource.com.auwrote:
On Wed, 04 Jul 2007 14:37:34 +0000, Marc 'BlackJack' Rintsch wrote:
>On Wed, 04 Jul 2007 09:59:24 -0400, David Abrahams wrote:
>>Here's an implementation of the functionality I propose, as a
free-standing function:

def intersects(s1,s2):
if len(s1) < len(s2):
for x in s1:
if x in s2: return True
else:
for x in s2:
if x in s1 return True
return False

In Python 2.5 this can be written a bit more concise:

def intersects(set_a, set_b):
if len(set_a) < len(set_b):
set_a, set_b = set_b, set_a
return any(item in set_a for item in set_b)


I'd rather see sets gain an method "isintersect()"
And why is that a good name? It's not even grammatical.
with the functionality than the language to gain a built-in
function.
How is a method on sets not a built-in function? Anyway, such a
method is what I'm proposing.
However, there's a very subtle flaw in the idea. While "the intersection"
of two sets is well-defined, "these two sets intersect" is (surprisingly!)
_not_ well-defined.
Depends how you define it. I define it as "has a non-empty
intersection," which is pretty obviously solid. The semantics should
be identical to

len(s1&s2) 0

Those are exactly the semantics I want, and if mathematical purists
are going to argue that "intersects" is the wrong name, I ask that
they come up with a better one.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==http://www.astoriaseminar.com

Jul 5 '07 #6
On Wed, 04 Jul 2007 23:53:15 -0400, David Abrahams wrote:
>
on Wed Jul 04 2007, "Steven D'Aprano" <steve-AT-REMOVE.THIS.cybersource.com.auwrote:
>On Wed, 04 Jul 2007 14:37:34 +0000, Marc 'BlackJack' Rintsch wrote:
>>On Wed, 04 Jul 2007 09:59:24 -0400, David Abrahams wrote:

Here's an implementation of the functionality I propose, as a
free-standing function:

def intersects(s1,s2):
if len(s1) < len(s2):
for x in s1:
if x in s2: return True
else:
for x in s2:
if x in s1 return True
return False

In Python 2.5 this can be written a bit more concise:

def intersects(set_a, set_b):
if len(set_a) < len(set_b):
set_a, set_b = set_b, set_a
return any(item in set_a for item in set_b)


I'd rather see sets gain an method "isintersect()"

And why is that a good name? It's not even grammatical.
Neither is "It's not even grammatical", but all but purists use it
regardless.

A common Python convention is to have test functions named something
like "isfoo", e.g. str.isdigit(), isspace(), islower() etc. They're not
exactly grammatical either, e.g. isdigit() should actually be "contains
one or more digits, and nothing but digits". (Presumably the pedantically
correct name was rejected as being too long.) I was just following that
convention.

My main feeling is that any such function should be a set method rather
than a built-in function like len(). The name change was comparatively
unimportant.

>with the functionality than the language to gain a built-in
function.

How is a method on sets not a built-in function? Anyway, such a
method is what I'm proposing.
Although they are similar, methods are not identical to functions, even
if many of us, myself included, often use the terms interchangeably.

len() is a built-in function. str.lower() is a method of strings. I'd
prefer to see intersects() be a method of sets than a built-in function,
and I think you'd have a much better chance of getting it approved as a
method than as a function, but I could be wrong.
>However, there's a very subtle flaw in the idea. While "the intersection"
of two sets is well-defined, "these two sets intersect" is (surprisingly!)
_not_ well-defined.

Depends how you define it.
Exactly my point.

I define it as "has a non-empty intersection," which is pretty obviously
solid.
I didn't say it was a bad choice, only that it isn't the only choice.
--
Steven.

Jul 5 '07 #7
Steven D'Aprano skrev:
On Wed, 04 Jul 2007 23:53:15 -0400, David Abrahams wrote:
>on Wed Jul 04 2007, "Steven D'Aprano" <steve-AT-REMOVE.THIS.cybersource.com.auwrote:
>>On Wed, 04 Jul 2007 14:37:34 +0000, Marc 'BlackJack' Rintsch wrote:

On Wed, 04 Jul 2007 09:59:24 -0400, David Abrahams wrote:

Here's an implementation of the functionality I propose, as a
free-standing function:
>
def intersects(s1,s2):
if len(s1) < len(s2):
for x in s1:
if x in s2: return True
else:
for x in s2:
if x in s1 return True
return False
In Python 2.5 this can be written a bit more concise:

def intersects(set_a, set_b):
if len(set_a) < len(set_b):
set_a, set_b = set_b, set_a
return any(item in set_a for item in set_b)

I'd rather see sets gain an method "isintersect()"
And why is that a good name? It's not even grammatical.

Neither is "It's not even grammatical", but all but purists use it
regardless.

A common Python convention is to have test functions named something
like "isfoo", e.g. str.isdigit(), isspace(), islower() etc. They're not
exactly grammatical either, e.g. isdigit() should actually be "contains
one or more digits, and nothing but digits". (Presumably the pedantically
correct name was rejected as being too long.) I was just following that
convention.
The problem is, these functions can be read as "X is [consisting only
of] digit[s]", "X is lower [case]" etc, where the bits in brackets have
been removed for brewity. In the case of "s1 is intersect s2" there is
no way I can see of adding words to get a correct sentence. The
"obvious" naming is "s1.intersects(s2)" which reads as "s1 intersects
s2", a perfectly cromulent sentence.

Nis
Jul 5 '07 #8
In article <pa****************************@REMOVE.THIS.cybers ource.com.au>,
Steven D'Aprano <st***@REMOVE.THIS.cybersource.com.auwrote:
>
My main feeling is that any such function should be a set method rather
than a built-in function like len(). The name change was comparatively
unimportant.
Look up at the Subject: line. There never was any suggestion that
intersects() be anything other than a set method.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

I support the RKAB
Jul 5 '07 #9
On Thu, 05 Jul 2007 07:34:28 -0700, Aahz wrote:
In article <pa****************************@REMOVE.THIS.cybers ource.com.au>,
Steven D'Aprano <st***@REMOVE.THIS.cybersource.com.auwrote:
>>
My main feeling is that any such function should be a set method rather
than a built-in function like len(). The name change was comparatively
unimportant.

Look up at the Subject: line.
Subject line? What's that?

*chagrined wink*
There never was any suggestion that
intersects() be anything other than a set method.
My fault for replying to a reply instead of the original post.
--
Steven.

Jul 5 '07 #10
On Thu, 05 Jul 2007 01:48:58 +0000, richyjsm wrote:
On Jul 4, 8:14 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
>However, there's a very subtle flaw in the idea. While "the intersection"
of two sets is well-defined, "these two sets intersect" is (surprisingly!)
_not_ well-defined.

Poppycock! It's perfectly well defined: two sets intersect if and
only if their intersection is nonempty.
That's certainly _a_ definition.

I'm not a professional set theorist, but in 15-odd years of studying and
teaching maths I've never come across mathematicians using intersect as a
verb except as informal short-hand. I often say "North Street and South
Street don't intersect", but "the intersection of sets A and B is empty".

Just because I've never come across it doesn't mean it exists, so I'd be
grateful for any reference to a technical definition, or even references
to any mathematician using intersect as a verb in a vigorous,
non-hand-waving way. Here's a link to get you started:

http://www.google.com.au/search?q=define%3Aintersect

There's absolutely no reason
to single out the empty set for special treatment in this definition.
But you can't avoid treating the empty set as special, because it _is_
special. You can only choose where to do so. For instance, do sets
intersect with themselves?

By my rule, every set intersects with itself, no exceptions.

By your rule, every set intersects with itself, except for the empty set.
>The problem comes if we (perhaps naively) try to say that if a set A is a
subset of set B, set A must intersect with B.

Well of course false statements are going to cause problems.
My statement is only false if we treat the empty set as special, which
you've said we shouldn't do.

By my rule: if A <= B, then A and B intersect, no exceptions.

By your rule: if A <= B and A is not the empty set, then A and B intersect.
>(Not all intersecting sets are subsets, but all subsets are
intersecting sets.)

Not true.
Can you give a counter-example, without treating the empty set as special?

Of course if you exclude the empty set by definition, my statement is no
longer true -- but that's begging the question of whether we should
exclude the empty set or not.

>As a result, any proposed function or method that returns a True/False
value for whether set A intersects with set B needs to define (and
justify) what it means to say that two sets intersect when one or both
are the empty set.

Nope. There's one, obvious, correct definition, as given above. No
need to mention the empty set at all.
It is obvious, but whether it is correct or not depends on how you decide
whether two things intersect or not.
--
Steven.

Jul 5 '07 #11
Steven D'Aprano wrote:
Just because I've never come across it doesn't mean it exists, so
I'd be grateful for any reference to a technical definition, or
even references to any mathematician using intersect as a verb in a
vigorous, non-hand-waving way. Here's a link to get you started:
Here ( http://arxiv.org/PS_cache/math/pdf/0702/0702029v1.pdf )is a
Russian math text (in English) which says:

"If A \cap B 6 = \emptyset, then we say that the sets A and B do
intersect. Otherwise, if A \cap B = \emptyset, then we say that these
sets do not intersect."

Here (
http://www.tcs.ifi.lmu.de/~fischerf/...bfh_tark07.pdf ) is a
math paper which says:

"Sets that are disjoint in the diagram may have an empty
intersection, i.e., there exist instances where these sets do not
intersect."

A simple Google search will also turn up numerous uses of the
phrase "non-intersecting sets", which would seem to be parallel (i.e.,
people are not bending over backwards to say "disjoint sets" or "sets
with empty intersection").

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
Jul 5 '07 #12
Nis Jørgensen wrote:
The problem is, these functions can be read as "X is [consisting only
of] digit[s]", "X is lower [case]" etc, where the bits in brackets have
been removed for brewity. In the case of "s1 is intersect s2" there is
no way I can see of adding words to get a correct sentence. The
"obvious" naming is "s1.intersects(s2)" which reads as "s1 intersects
s2", a perfectly cromulent sentence.
Agree. A possible alternative would be "s1.hasintersection(s2)", but
s1.intersects(s2) is ok as well.

-- Chris
Jul 5 '07 #13
Steven D'Aprano wrote:
I'm not a professional set theorist, but in 15-odd years of studying and
teaching maths I've never come across mathematicians using intersect as a
verb except as informal short-hand. I often say "North Street and South
Street don't intersect", but "the intersection of sets A and B is empty".
I think mathematicians use more often the inverse predicate, namely
"disjoint", which is well defined as having an empty intersection.

-- Chris
Jul 5 '07 #14

on Thu Jul 05 2007, Christoph Zwerschke <cito-AT-online.dewrote:
Steven D'Aprano wrote:
>I'm not a professional set theorist, but in 15-odd years of studying and
teaching maths I've never come across mathematicians using intersect as a
verb except as informal short-hand. I often say "North Street and South
Street don't intersect", but "the intersection of sets A and B is empty".

I think mathematicians use more often the inverse predicate, namely
"disjoint", which is well defined as having an empty intersection.
Doesn't read so well as a method, though. You end up with
"a.is_disjoint_with(b)."

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==http://www.astoriaseminar.com

Jul 6 '07 #15

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

Similar topics

29
by: David MacQuigg | last post by:
On 27 Apr 2004 16:34:56 -0700, has.temp2@virgin.net (has) wrote: >David MacQuigg <dmq@gain.com> wrote in message news:<bniq80hiib0gauiltuntk9jvia2getbnj4@4ax.com>... > >> Example of Simplified...
12
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
7
by: jacob navia | last post by:
Microsoft proposed recently (In the Technical Report 24173 presented to the Standards Comitee) a change in the C library in the sense of more security. Basically, missing size information is...
4
by: MonkeeSage | last post by:
Proposal: When an attribute lookup fails for an object, check the top-level (and local scope?) for a corresponding function or attribute and apply it as the called attribute if found, drop...
1
by: Marv1n | last post by:
Hi, I'm trying to do several things with the goal of automating some reporting, and am not sure what the right tools to use are, so maybe you folks can provide some clarity for me. I'm trying my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.