Saturday, May 6, 2023

#PYTHON(DAY10)Methods unique to sets

 

#PYTHON(DAY10)

Methods unique to sets

The methods in sets are:

  1. union()
  2. Intersection
  3. difference
  4. symmetric_difference
  5. intersection_update
  6. difference_update
  7. symmetric_difference_update
  8. isdisjoint, issubset, issuperset

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.

union of sets
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}

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.

Intersection of 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}
  • 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.

difference of 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}

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}

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}

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}

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}

The isdisjoint() method returns True if none of the items are present in both sets, otherwise it returns False.

disjoint set
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.isdisjoint(y)

print(z)

And the output is:

False

The issubset() method returns True if all items in the set exists in the specified set, otherwise it returns False.

subset
x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b"}

z = x.issubset(y)

print(z)

And the output is:

False

The issuperset() method returns True if all items in the specified set exists in the original set, otherwise it returns False.

superset
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

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...