472,958 Members | 2,127 Online

# about data structure --- 'Set'

Hello everyone,

If you look at the following tutorial, you will find the great advantage of using 'set' as a data structure.

http://docs.python.org/tut/node7.htm...00000000000000

But it seems I need to import something before using it, otherwise it'll not be recognised. Could anybody please tell me what I should import?

Oct 31 '07 #1
2 1534
bartonc
6,596 Expert 4TB
Hello everyone,

If you look at the following tutorial, you will find the great advantage of using 'set' as a data structure.

http://docs.python.org/tut/node7.htm...00000000000000

But it seems I need to import something before using it, otherwise it'll not be recognised. Could anybody please tell me what I should import?

If your Python version is less than 2.4:
Expand|Select|Wrap|Line Numbers
1. import Set as set
2. # or (I think)
3. from Future import Set as set
Use the as modifier so that you code works with higher versions.

Otherwise, it's as simple as
Expand|Select|Wrap|Line Numbers
1. >>> set([1,1,3,4,5,5])
2. set([1, 3, 4, 5])
3. >>>
Note that sets are not mutable (just like tuples). I've been using sets lately for their cool ability to subtract from one another:
Expand|Select|Wrap|Line Numbers
1. >>> set([1,2,3,4,5,6]) - set([4,5,6])
2. set([1, 2, 3])
3. >>>
Oct 31 '07 #2
If your Python version is less than 2.4:
Expand|Select|Wrap|Line Numbers
1. import Set as set
2. # or (I think)
3. from Future import Set as set
Use the as modifier so that you code works with higher versions.

Otherwise, it's as simple as
Expand|Select|Wrap|Line Numbers
1. >>> set([1,1,3,4,5,5])
2. set([1, 3, 4, 5])
3. >>>
Note that sets are not mutable (just like tuples). I've been using sets lately for their cool ability to subtract from one another:
Expand|Select|Wrap|Line Numbers
1. >>> set([1,2,3,4,5,6]) - set([4,5,6])
2. set([1, 2, 3])
3. >>>

cool! thanks a lot. the python version i used was 2.2.... that's why...
Nov 1 '07 #3