Saturday, 1 April 2017

Collection data type: set

set:

  • Set object can be created by using curly brackets { },or by calling set function.
  • Insertion order is not preserved.
  • Duplicate elements are not allowed.
  • Heterogeneous elements are allowed.
  • Set is a mutable object but the elements must be immutable.
  • Set object doesn't support indexing.
  • We can perform the mathematical set operations like union, intersection, difference, symmetric difference on set objects.
               a={10,20,30}
               print(a)
               print(type(a))

              {10, 20, 30}
             <class 'set'>


               b=set()
               print(b)
               print(type(b))

               set()
             <class 'set'>


           c={1,2.3,4+3j,True,"sivakrishna"}
           print(c)

          {1, 2.3, 'sivakrishna', (4+3j)}


          d={10,20,10,30,20}
          print(d)

         {10, 20, 30}

  • list inside list is allowed
  • tuple inside tuple is allowed
  • But set inside set is not allowed
  • set inside list not support
  • set inside tuple support

                  e={{1,2,3},{'siva','krishna'}}
                  print(e)

                 Traceback (most recent call last):
                 File "E:/python_practice/examples/set.py", line 1, in <module>
                                                       e={{1,2,3},{'siva','krishna'}}
                TypeError: unhashable type: 'set'



               f={[10,20,30],['siva','krishna']}
               print(f)

               Traceback (most recent call last):
               File "E:/python_practice/examples/set.py", line 1, in <module>
                              f={[10,20,30],['siva','krishna']}
              TypeError: unhashable type: 'list'

              g={(10,20,30),('siva','krishna')}
              print(g)

             {(10, 20, 30), ('siva', 'krishna')}

  • In set object  not perform while loop, here perform for loop. because set object not support indexing.
  • for loop working not based on indexes
  • while loop working based on indexes
                     a={10,20,30}
                     print(a)
                     print(type(a))
                     print(a[2])

                     Traceback (most recent call last):
                     File "E:/python_practice/examples/set.py", line 4, in <module>
                                               print(a[2])
                    TypeError: 'set' object does not support indexing


                    x={(1,2,3),('siva','krishna'),(3+4j,True)}
                    print(x)
                   for p in x:
                          print(p,type(p))


                   {((3+4j), True), ('siva', 'krishna'), (1, 2, 3)}
                   ((3+4j), True)  <class 'tuple'>
                   ('siva', 'krishna')  <class 'tuple'>
                   (1, 2, 3)  <class 'tuple'>


                   y={10,20,30,40,50}
                   print(y)
                   print(len(y))
                   i=0
                  while i<len(y):
                              print(y[i])
                              i=i+1

                {40, 10, 20, 50, 30}
                 5
               Traceback (most recent call last):
               File "E:/python_practice/examples/set.py", line 4, in <module>
                                       print(y[i])
               TypeError: 'set' object does not support indexing

Python Set Methods
There are many set methods, some of which we have already used above. Here is a list of all the methods that are available with set objects.


Python Set Methods
Method
Description
add()
Add an element to a set
clear()
Remove all elemets form a set
copy()
Return a shallow copy of a set
difference()
Return the difference of two or more sets as a new set
difference_update()
Remove all elements of another set from this set
discard()
Remove an element from set if it is a member. (Do nothing if the element is not in set)
intersection()
Return the intersection of two sets as a new set
intersection_update()
Update the set with the intersection of itself and another
isdisjoint()
Return True if two sets have a null intersection
issubset()
Return True if another set contains this set
issuperset()
Return True if this set contains another set
pop()
Remove and return an arbitrary set element. Raise KeyError if the set is empty
remove()
Remove an element from a set. It the element is not a member, raise a KeyError
symmetric_difference()
Return the symmetric difference of two sets as a new set
symmetric_difference_update()
Update a set with the symmetric difference of itself and another
union()
Return the union of sets in a new set
update()
Update a set with the union of itself and others

                            A={1,2,3,4,5}
                            print(A)
                           print(len(A))
                          {1, 2, 3, 4, 5}
                          5

                          A.add(6)
                          print(A)
                        {1, 2, 3, 4, 5, 6}


                       print(A.copy())
                      {1, 2, 3, 4, 5, 6}

                      A.discard(3)
                      print(A)
                     {1, 2, 4, 5, 6}

                      print(A.pop())
                      1

                    print(A)
                   {2, 4, 5, 6}

                    A.remove(2)
                    print(A)
                    {4, 5, 6}

                     A.clear()
                     print(A)
                     print(len(A))
                     set( )
                      0
union

a={1,2,3,4,5}
b={4,5,6,7,8}
print(a|b)
print(a.union(b))
print(b|a)
print(b.union(a))

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

intersection

a={1,2,3,4,5}
b={4,5,6,7,8}
print(a&b)
print(a.intersection(b))
print(b&a)
print(b.intersection(a))

{4, 5}
{4, 5}
{4, 5}
{4, 5}

difference

a={1,2,3,4,5}
b={4,5,6,7,8}
print(a-b)
print(a.difference(b))
print(b-a)
print(b.difference(a))

{1, 2, 3}
{1, 2, 3}
{8, 6, 7}
{8, 6, 7}

symmetric difference

a={1,2,3,4,5}
b={4,5,6,7,8}
print(a^b)
print(a.symmetric_difference(b))
print(b^a)
print(b.symmetric_difference(a))

{1, 2, 3, 6, 7, 8}
{1, 2, 3, 6, 7, 8}
{1, 2, 3, 6, 7, 8}
{1, 2, 3, 6, 7, 8}

Note:
set support comprehension concept

s = { x for x in range(10) }
print(s)

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}



No comments:

Post a Comment