Proposal: s1.intersects(s2)

Marc 'BlackJack' Rintsch bj_666 at
Wed Jul 4 16:37:34 CEST 2007

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)

	Marc 'BlackJack' Rintsch

More information about the Python-list mailing list