about data structure --- 'Set'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wxy212
    New Member
    • Oct 2007
    • 5

    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.



    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!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by wxy212
    Hello everyone,

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



    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:[CODE=python]import Set as set
    # or (I think)
    from Future import Set as set[/CODE]Use the as modifier so that you code works with higher versions.

    Otherwise, it's as simple as[CODE=python]
    >>> set([1,1,3,4,5,5])
    set([1, 3, 4, 5])
    >>> [/CODE]Note that sets are not mutable (just like tuples). I've been using sets lately for their cool ability to subtract from one another:[CODE=python]
    >>> set([1,2,3,4,5,6]) - set([4,5,6])
    set([1, 2, 3])
    >>> [/CODE]
    Last edited by bartonc; Oct 31 '07, 07:48 PM.

    Comment

    • wxy212
      New Member
      • Oct 2007
      • 5

      #3
      Originally posted by bartonc
      If your Python version is less than 2.4:[CODE=python]import Set as set
      # or (I think)
      from Future import Set as set[/CODE]Use the as modifier so that you code works with higher versions.

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

      cool! thanks a lot. the python version i used was 2.2.... that's why...

      Comment

      Working...