Python set operation.

PythonProgrammingServer Side Programming

In mathematics, a set is a collection of distinct objects, considered as an object in its own right. For example, the numbers 2, 4, and 6 are distinct objects when considered separately, but when they are considered collectively they form a single set of size three, written {2,4,6}.

Operations on Sets

Operation Notation Meaning
Intersection A ∩ B all elements which are in both and
Union A ∪ B all elements which are in either or (or both)
Difference A − B all elements which are in but not in
Complement (or) all elements which are not in

In python, compared to list, the main advantages of using a set are that it has optimized functions for checking whether a specific element is a member of the set or not. This is based on a hash table data structure.

Methods for Sets

add(x) Method: It adds the item x to a set if it is non-preexisting.

A = {"AA", "BB", "CC"}
A.add("VV")

This will add VV in A set.

union(s) Method: It returns a union of two set.Using the operator ‘|’between 2 existing sets is the same as writing My_Set1.union(My_Set2)..

A = {"AA", "BB", "CC"}
B = {"MM", "NN"}
Z = A.union(B)
OR
Z = A|B

Set population set will have components of both A and B.

intersect(s) Method: It returns an intersection of two givensets. The ‘&’ operator can be used in this operation.

S = A.intersection(B)

Set victims will contain the common elements of A and B.

difference(s) Method: Returns a set containing all the elements which are existing in the first set but not present in the second set. We can use ‘-‘ operator here.

W = A.difference(B)
OR
S = A – B

Set safe will have all the elements that are in A but not in B.

clear() Method: The whole existing set will become empty.

B.clear()

Clears B set

Operators for Sets

Sets and frozen sets support the following operators -

key in s         # containment check
key not in s   # non-containment check
s1 == s2       # s1 is equivalent to s2
s1 != s2       # s1 is not equivalent to s2
s1 <= s2    # s1is subset of s2 s1 < s2     # s1 is proper subset of s2 s1 >= s2             # s1is superset of s2
s1 > s2     # s1 is proper superset of s2
s1 | s2        # the union of s1 and s2
s1 & s2        # the intersection of s1 and s2
s1 – s2        # the set of elements in s1 but not s2
s1 ˆ s2        # the set of elements in precisely one of s1 or s2

Example Code

# Python program to demonstrate working# of 
# Set in Python 
# creating two sets 
My_Set1 = set()
My_Set2 = set()
# Adding elements to My_Set1
for i in range(1, 6): 
   My_Set1.add(i) 
# Adding elements to My_Set2
for i in range(3, 8): 
   My_Set2.add(i) 
print("My_Set1 = ", My_Set1) 
print("My_Set2 = ", My_Set2) 
print("\n") 
# Union of My_Set1 and My_Set2
My_Set3 = My_Set1 | My_Set2# My_Set1.union(My_Set2) 
print("Union of My_Set1&My_Set2: My_Set3 = ", My_Set3) 
# Intersection of My_Set1 and My_Set2
My_Set4 = My_Set1&My_Set2# My_Set1.intersection(My_Set2) 
print("Intersection of My_Set1&My_Set2: My_Set4 = ", My_Set4) 
print("\n") 
# Checking relation between My_Set3 and My_Set4
if My_Set3>My_Set4: # My_Set3.issuperset(My_Set4) 
   print("My_Set3 is superset of My_Set4") 
elif My_Set3<My_Set4: # My_Set3.issubset(My_Set4) 
   print("My_Set3 is subset of My_Set4") 
else : # My_Set3 == My_Set4
   print("My_Set3 is same as My_Set4") 
# displaying relation between My_Set4 and My_Set3
if My_Set4<My_Set3: # My_Set4.issubset(My_Set3) 
   print("My_Set4 is subset of My_Set3") 
   print("\n") 
# difference between My_Set3 and My_Set4
My_Set5 = My_Set3 - My_Set4
print("Elements in My_Set3 and not in My_Set4: My_Set5 = ", My_Set5) 
print("\n") 
# check if My_Set4 and My_Set5 are disjoint sets 
if My_Set4.isdisjoint(My_Set5): 
   print("My_Set4 and My_Set5 have nothing in common\n") 
# Removing all the values of My_Set5
My_Set5.clear()
print("After applying clear on sets My_Set5: ") 
print("My_Set5 = ", My_Set5) 

Output

My_Set1 = {1, 2, 3, 4, 5}
My_Set2 = {3, 4, 5, 6, 7}
Union of My_Set1&My_Set2: My_Set3 = {1, 2, 3, 4, 5, 6, 7}
Intersection of My_Set1&My_Set2: My_Set4 = {3, 4, 5}
My_Set3 is superset of My_Set4
My_Set4 is subset of My_Set3
Elements in My_Set3 and not in My_Set4: My_Set5 = {1, 2, 6, 7}
My_Set4 and My_Set5 have nothing in common
After applying clear on sets My_Set5: 
My_Set5 = set()
raja
Published on 25-Sep-2018 13:41:59
Advertisements