Python3 Sets DS

Python Data Structures:  Sets

Sets :

  -> A set is an unordered collection of items. Each element is unique (no duplicates).
  -> The set itself is mutable .We can add or remove items from it.
  -> Sets can be used to perform mathematical set operations like union ,intersection ,symmetric ,difference etc.

Set Creation :

Eg:
       #set of integers
       s={1,2,3}
       print(s)
       print(type(s))

output:  set([1,2,3])
              < type 'set' >

       #set does not allow duplicates. They store only one instance.
       s={1,2,3,1,2}
       print(s)

 output:   {1,2,3}

Eg:
       #we can make set from a list
       s=set([1,2,3,1])
       print(s)

output:  {1,2,3}

Eg:
       #initialize a set with set( ) method
       s=set( )
       print(type(s))

 output:   < class 'set' >

Add element to a Set :

Eg:
       #we can add single element using add( ) method and 
       #add multiple elements using update( ) method
       s={1,2,3}
  
       #set object can't  support indexing
       print(s[1])

 output:  -----------------------------------------------------------------
                --
                TypeError 
                TypeError : 'set' object does not support indexing

Eg:
       #add element
       s={1,2,3}
       s.add(4)
       print(s)

       #add multiple elements
       s.update([5,6,1])
       print(s)

       #add list and set
       s.update([7,9],{10,2,3})
       print(s)

output:   {1,2,3,4}
                {1,2,3,4,5,6}
                {1,2,3,4,5,6,7,9,10}

Remove elements from a Set :

       #A particular item can be removed from set using methods,
       #discard( ) and remove( ).

Eg:
       s={1,3,2,4,5}
       print(s)

       s.discard(3)
       print(s)

output:   {1,2,3,4,5}
                {1,2,4,5}

       #remove an element
       s.remove(4)
       print(s)

output:   {1,2,5}

       #remove an element not present in a set s
              s.remove(7)
     
 output:  ----------------------------------------------------------------------
                --
                KeyError
                KeyError: 7

       #discard an element not present in a set s
       s.discard(7)
       print(s)

output:    {1,2,5}

       #we can remove item using pop( ) method
       s={1,2,3,4,5}
       s.pop( )              #remove random element
       print(s)
       s.pop( )
       print(s)

output:   {2,3,4,5}
                {3,4,5}

       #remove all item in set using clear( ) method 
       s={1,2,3,5,6}
       s.clear( )              
       print(s)

output:  set( )

Python Set Operations :

       set1={1,3,2,4,5}
       set2={3,4,6,7,8}

       #union of 2 sets using | operator
       print(set1 | set2)

output:   {1,2,3,4,5,6,7,8}

       #another way of getting union of 2 sets
       print(set1.union(set2))

 output:   {1,2,3,4,5,6,7,8}

       #intersection of 2 sets using & operator
       print(set1 & set2)

output:  {3,4}

       #use intersection function
       print(set1.intersection(set2))

 output:   {3,4}

       #Set Difference: set of elements that are only in set1 but not in set2
       print(set1 - set2)

output:  {1,2,5}

       #use difference function
       print(set1.difference(set2))

output:  {1,2,5}

       """ symmetric difference: set of elements in both set1 and set2
       #except those that are common in both"""
   
       #use ^ operator 
       print(set1 ^ set2)
 
output:    {1,2,5,6,7,8}

       #use symmetric_difference function
       print(set1.symmetric_difference(set2))
 
output:  {1,2,5,6,7,8}

Eg:
       #find issubset( )
       x={"a","b","c","d"}
       y={"a","b","c"}
       print("set 'x' is subset of 'y' ?",x.issubset(y))
       print("set 'y' is subset  of 'x' ?",y.issubset(x))

output:  set 'x' is subset of 'y' ? False
               set 'y' is subset of 'x' ? True

Frozen Sets :
 
    Frozen sets have characteristics of sets, but we can't be changed once it's assigned. While tuple are immutable lists, frozen sets are immutable sets.

    Frozen can be created using the function frozenset()

    Set being mutable are unhashable, so they can't be used as dictionary keys. On the other hand, frozensets are hashable and can be used as keys to a dictionary.

   This datatype support methods like copy(), difference(),
intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and union(). Being immutable it can't have method that add or remove elements.

Eg:
       set1=frozenset([1,2,3,4])
       set2=frozenset([3,4,5,6])

       st1.add(5)

 output:  -----------------------------------------------------------------
               AttributeError
               AttributeError: 'frozenset' object has no attribute 'add'

      
       ***it does not support indexing also.

       print(set1 | set2)
       print(set1 & set2)
       print(set1 ^ set2)

output:  frozenset({1,2,3,4,5,6})
               frozenset({3,4})
               frozenset({1,2,5,6})



       




Comments

Popular posts from this blog

Python3 Function Arguments

Python3 File Handling

Python3