[Tutor] sets module equivalent with Python 2.6

wesley chun wescpy at gmail.com
Tue Mar 17 22:48:51 CET 2009


On Tue, Mar 17, 2009 at 2:34 PM, PyProg PyProg <pyprog05 at gmail.com> wrote:
>
> I want to use an equivalent of sets module with Python 2.6 ... but
> sets module is deprecated on 2.6 version.

it is deprecated only because sets have been rolled into Python proper
starting in 2.4. replace sets.Set() with set(), and there is
frozenset() as well.


> In fact I want to make that but with Python 2.6:
>
>>>> toto_1 = [0, 1, 2, 3, 4]
>>>> toto_2 = [1, 127, 4, 7, 12]
>>>>
>>>> import sets
>>>> a = sets.Set(toto_1)
>>>> b = sets.Set(toto_2)
>>>> c = a.symmetric_difference(b)
>>>> d = [p for p in c]
>>>> d.sort()
>>>> print d
> [0, 2, 3, 7, 12, 127]
>
> Can you help me ?.

here is some code that works under Python 2.4, 2.5, and 2.6:
$ python2.6
Python 2.6.1 (r261:67515, Feb 26 2009, 01:19:46)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> toto_1 = [0, 1, 2, 3, 4]
>>> toto_2 = [1, 127, 4, 7, 12]
>>>
>>> a = set(toto_1)
>>> b = set(toto_2)
>>> c = a.symmetric_difference(b)
>>> d = [p for p in c]
>>> d.sort()
>>> print d
[0, 2, 3, 7, 12, 127]

in 3.x, you also get set literals, meaning no more factory function
calls are required!

$ python3.0
Python 3.0.1 (r301:69556, Feb 26 2009, 01:01:25)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = {0, 1, 2, 3, 4}
>>> b = {1, 127, 4, 7, 12}
>>> c = a.symmetric_difference(b)
>>> d = [p for p in c]
>>> d.sort()
>>> print(d)
[0, 2, 3, 7, 12, 127]

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list