Proposal: s1.intersects(s2)

Thomas Jollans thomas at jollans.com
Wed Jul 4 10:18:58 EDT 2007


On Wednesday 04 July 2007, 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
>
> 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.

>>> s1 = set(xrange(5))
>>> s2 = set(xrange(3,9))
>>> s1
set([0, 1, 2, 3, 4])
>>> s2
set([3, 4, 5, 6, 7, 8])
>>> s1 | s2
set([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> s1 & s2
set([3, 4])
>>>

It's all in python already. And documented on the web too.

-- 
      Regards,                       Thomas Jollans
GPG key: 0xF421434B may be found on various keyservers, eg pgp.mit.edu
Hacker key <http://hackerkey.com/>:
v4sw6+8Yhw4/5ln3pr5Ock2ma2u7Lw2Nl7Di2e2t3/4TMb6HOPTen5/6g5OPa1XsMr9p-7/-6
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/python-list/attachments/20070704/dd8fa8e9/attachment.sig>


More information about the Python-list mailing list