#PYTHON(DAY10)
Methods unique to sets
The methods in sets are:
- union()
- Intersection
- difference
- symmetric_difference
- intersection_update
- difference_update
- symmetric_difference_update
- isdisjoint, issubset, issuperset
union()
The union() method returns a set that contains all items from the original set, and all items from the specified set(s).
You can specify as many sets you want, separated by commas.
It does not have to be a set, it can be any iterable object.
If an item is present in more than one set, the result will contain only one appearance of this item.
a = set({1,2,3,4,5})
b = set({4,5,6,7,8})
c = a.union(b)
d = b.union(a)
print(a,b,c,d, sep = '\n')
And the output is:
{1, 2, 3, 4, 5}
{4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8}
intersection()
The intersection() method returns a set that contains the similarity between two or more sets.
Meaning: The returned set contains only items that exist in both sets, or in all sets if the comparison is done with more than two sets.
a = set({1,2,3,4,5})
b = set({4,5,6,7,8})
c = a.intersection(b)
d = b.intersection(a)
print(a,b,c,d, sep = '\n')
And the output is:
{1, 2, 3, 4, 5}
{4, 5, 6, 7, 8}
{4, 5}
{4, 5}
difference()
- it just removes the common elements in the set
The difference() method returns a set that contains the difference between two sets.
Meaning: The returned set contains items that exist only in the first set, and not in both sets.
a = set({1,2,3,4,5})
b = set({4,5,6,7,8})
c = a.difference(b)
d = b.difference(a)
print(a,b,c,d, sep = '\n')
And the output is:
{1, 2, 3, 4, 5}
{4, 5, 6, 7, 8}
{1, 2, 3}
{8, 6, 7}
Symmetric_difference():
it removes the common elements from the set and gives the combination of remaining elements in the set
The symmetric_difference() method returns a set that contains all items from both set, but not the items that are present in both sets.
Meaning: The returned set contains a mix of items that are not present in both sets.
a = set({1,2,3,4,5})
b = set({4,5,6,7,8})
c = a.symmetric_difference(b)
d = b.symmetric_difference(a)
e = set()
print(a,b,c,d, sep = '\n')
And the output is:
{1, 2, 3, 4, 5}
{4, 5, 6, 7, 8}
{1, 2, 3, 6, 7, 8}
{1, 2, 3, 6, 7, 8}
intersection_update():
The intersection_update() method removes the items that is not present in both sets (or in all sets if the comparison is done between more than two sets).
The intersection_update() method is different from the intersection() method, because the intersection() method returns a new set, without the unwanted items, and the intersection_update() method removes the unwanted items from the original set.
a = set({1,2,3,4,5})
b = set({4,5,6,7,8})
print(a,b, sep = '\n')
a.intersection_update(b)
b.intersection_update(a)
print(a,b, sep = '\n')
And the output is:
{1, 2, 3, 4, 5}
{4, 5, 6, 7, 8}
{4, 5}
{4, 5}
difference_update():
It will calculate the difference of twosets and update the value of set, it is called upon
The difference_update() method removes the items that exist in both sets.
The difference_update() method is different from the difference() method, because the difference() method returns a new set, without the unwanted items, and the difference_update() method removes the unwanted items from the original set.
a = set({1,2,3,4,5})
b = set({4,5,6,7,8})
print(a,b, sep = '\n')
a.difference_update(b)
b.difference_update(a)
print(a,b, sep = '\n')
And the output is:
{1, 2, 3, 4, 5}
{4, 5, 6, 7, 8}
{1, 2, 3}
{4, 5, 6, 7, 8}
symmetric_difference_update():
It will calculate the symmetric_difference of twosets and update the value of set, it is called upon
The symmetric_difference_update() method updates the original set by removing items that are present in both sets, and inserting the other items.
a = set({1,2,3,4,5})
b = set({4,5,6,7,8})
print(a,b, sep = '\n')
a.symmetric_difference_update(b)
print(a,b, sep = '\n')
And the output is:
{1, 2, 3, 4, 5}
{4, 5, 6, 7, 8}
{1, 2, 3, 6, 7, 8}
{4, 5, 6, 7, 8}
isdisjoint():
The isdisjoint() method returns True if none of the items are present in both sets, otherwise it returns False.
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.isdisjoint(y)
print(z)
And the output is:
False
issubset():
The issubset() method returns True if all items in the set exists in the specified set, otherwise it returns False.
x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b"}
z = x.issubset(y)
print(z)
And the output is:
False
issuperset():
The issuperset() method returns True if all items in the specified set exists in the original set, otherwise it returns False.
x = {"f", "e", "d", "c", "b"}
y = {"a", "b", "c"}
z = x.issuperset(y)
print(z)
And the output is:
False
No comments:
Post a Comment