471,312 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,312 software developers and data experts.

about data structure --- 'Set'

5
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?

Thanks in advance!
Oct 31 '07 #1
2 1484
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?

Thanks in advance!
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
wxy212
5
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by kazack | last post: by
1 post views Thread by Andrew Arace | last post: by
4 posts views Thread by Thomas Paul Diffenbach | last post: by
3 posts views Thread by aurora | last post: by

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.