Python. Classes set, frozenset. Functions for working with sets. Purpose, examples of use




Classes set, frozenset. Functions for working with sets. Purpose, examples of use


Contents


Search other websites:

1. The purpose of the classes set, frozenset

The set class is used to create and process mutable sets. The frozenset class is designed to create immutable or hashable sets.

The set, frozenset classes implement operations and functions for working with sets. These operations and functions are standard and you do not need to connect additional modules to use them.

All functions can be divided into the following categories:

  • functions that determine the characteristics of one or more sets (for example, the len function and others);
  • functions that form a new set by performing some operation on other sets (for example, union, intersection, and others);
  • functions that define features in several sets (for example, issubset, issuperset and others);
  • functions that change the current set (for example, add, update, intersection_update, and others).

 

2. Function len(). The number of elements in the set

The len() function returns the number of elements in the set (cardinality of the set). General form of function description

k = len(s)

where

  • s – some set;
  • k – the number of elements in the set s.

Example.

# Sets
# Function len

x = { 1, 3, 'abcd', 2.85 }
length = len(x) # length = 4 - the number of elements in the set

# empty set
length = len({}) # length = 0 - empty set

 

3. Function isdisjoint(). Determining the presence (absence) of common elements in sets

The isdisjoint() function returns True if the given set has no elements in common with another. According to Python documentation, the general form of a function call is as follows

set.isdisjoint(other)

where

  • set – some set (an instance of the set class);
  • other – the set with which the set is compared.

Example.

# Sets
# Function isdisjont()

# sets have common elements
x = { 1, 3, 'abcd', 2.85 }
y = { 2, -4, 1 }
f = x.isdisjoint(y) # f = False - common element 1

# sets do not have common elements
x = { 2, 3, 4 }
y = { 7 }
f = x.isdisjoint(y) # f = True
f = y.isdisjoint(x) # f = True

# could be so
f = { 'a', 'b', 'f' }.isdisjoint({ 'c', 'z' }) # f = True

 

4. Function union(). Union of sets

The union() function is used to union sets. The general form of a function call is as follows

union_set = set.union(other)

where

  • set, other – union sets;
  • union_set – the set that is the result.

The function returns the union set. The call of this function can be replaced by the operation of combining the sets |.

Example.

# Sets
# Function union() - The union of sets

x = { 1.25, -12.232 }
y = { -3.88, 2.66 }
z = x.union(y) # z = { 1.25, -12.232, -3.88, 2.66 }
v = y.union(x) # v = { 1.25, 2.66, -3.88, -12.232 }

z = {2, 3}.union({3, 5}.union({7, 9})) # z = {2, 3, 5, 7, 9}


 

5. Function intersection(). Intersection of sets

The intersection() function implements the so-called “intersection of sets”. When intersection of sets, a new set is obtained that contains elements common to two sets.

According to Python documentation, the general form of the function is as follows

intersection_set = set.intersection(*others)

where

  • set – current set;
  • *others – one or more sets. The intersection() function returns the newly created set, which is the result of the intersection of the set and others;
  • intersection_set – the resulting set.

In a program, a function can also be replaced with the & operation.

Example.

# Sets
# Function intersection() - intersection of sets

# intersection of 2 sets
x = { 2, 4, 6, 8 }
y = { 2, 8 }
z = x.intersection(y) # z = {8, 2}

# intersection of 4 sets
x1 = { 1, 2, 4, 5, 7, 9 }
x2 = { 1, 2, 3, 5, 4, 2, 8 }
x3 = { 1, 3, 4, 5, 7, 9 }
x4 = { 1, 2, 3, 4, 8, 9 }
z = x1.intersection(x2,x3,x4) # z = {1,4}

 

6. Function difference(). The difference of sets

The difference() function implements the difference of sets. The general form of using the function is as follows

difference_set = set.difference(*others)

where

  • set – a set of elements which must be subtracted from the set (or sets) others;
  • others – some set or several sets separated by a comma;
  • difference_set – the resulting set.

The difference() function can be replaced by the operation (minus).

Example.

# Sets
# Function difference() - the difference of sets

# difference of 2 sets
x = { 2, 4, 6, 8 }
y = { 2, 8 }
z = x.difference(y) # z = {4, 6}
v = y.difference(x) # v = {}

# difference of 3 sets
x = { 1, 2, 3, 4, 5, 6 }
y = { 3, 5 }
z = { 2, 4, 5 }
v = x.difference(y,z) # v = {1, 6}

 

7. Function symmetric_difference(). Symmetric difference

The symmetric_difference() function implements a symmetric difference in which the elements common to these sets are subtracted from the two sets. The general form of the function is as follows

symm_differ_set = set.symmetric_difference(other)

where

  • set, other – considered sets;
  • symm_differ_set – the resulting set.

The function returns a new set that does not contain elements common to both sets set and other. This function can be replaced by a symmetric difference operator.

Example.

# Sets
# Function symmetric_difference()

x = { 1, 2, 3, 4, 5 }
y = { 1, 3, 5 }
z = x.symmetric_difference(y) # z = {2, 4}

 

8. Function copy(). The copy of sets

The copy() function copies sets. The function does not receive parameters. The function returns a copy of the set. General form of function

copy_set = set.copy()

where

  • set – the current set to make a copy of;
  • copy_set – the resulting set-copy.

Example.

# Sets
# Function copy() - copy of the set

x = { 1, 3, 8 }
y = x.copy() # y = { 8, 1, 3 }

z = { 5, 6, 7 }.copy() # z = { 5, 6, 7 }

 

9. Function update(). Updating a set using the add operation

The update() function updates the set by adding new elements to it. The general form of using the function is as follows

set.update(*others)

where

  • set – the set to be updated;
  • others – the set or sets that are added to the set.

The function updates the current set, so it is not called on the right side of the assignment operator. This function performs the operation |= on sets.

Example.

# Sets
# Function update()

# 1. Add one set
x = { 5, 6 }
x.update({2, 8, 10}) # add elements 2, 8, 10 to the set x

# 2. Add one or more sets
x = { 7 }
y = { 1, 5, 9 }
x.update(y) # x = {1, 9, 5, 7}
x.update({15}, {2}, {'abc'}) # x = {1, 2, 'abc', 5, 7, 9, 15}

 

10. Function intersection_update(). Updating a set using an intersection operation

The intersection_update() function updates the current set using intersection sets. The general form of using the function is as follows

set.intersection_update(*others)

where

  • set – the set that is being updated;
  • others – one or more sets to be updated using the intersection operation.

This function performs the &= operation on sets.

Example.

# Sets
# Function intersection_update()

# Updating the current set using intersection
x = { 1, 2, 3, 4, 5, 6, 7 }
x.intersection_update({3,4,5})

# The case with multiple sets
x = { 1, 2, 3, 4, 5, 6, 7 }
x.intersection_update({5,6,7,8}, {1,2,6,8}) # x = {6}

 

11. Function difference_update(). Designed to obtain the difference of sets

The difference_update() function implements updating a set using the operation of subtracting sets. General form of using a function

set.difference_update(*others)

where

  • set – the set that is being updated;
  • others – one or more sets that are sequentially subtracted from the set.

The difference_update() function can be replaced by one or more operations (subtraction of sets). The function replaces the operation –=.

Example.

# Sets
# Function difference_update()

# Updating the current set using subtraction
x = { 1, 2, 3, 4, 5, 6, 7, 8, 9}
y = { 2, 4, 6, 8 }
x.difference_update(y) # x = {1, 3, 5, 7, 9}

# Subtracting of sets
x = set('abcdef') # x = {'a', 'c', 'b', 'e', 'd', 'f'}
x.difference_update({'c'}, {'d'}, {'c','e'}) # x = {'a','b','f'}

 

12. Function add(). Add item to the set

The add() function is intended to add an element to a set. The general form of using the function is as follows

set.add(elem)

here set is the set to which the elem element is added. An element can be of any simple type.

Example.

# Sets
# Function add() - add item to set

# form a set of integers in a loop using add()
n = int(input("Enter number:"))

i=1
x = set([]) # x = { } - first is empty set
while i<=n:
    x.add(i) # invoke the function add()
    i=i+1

# Display x
print('x = ', x)

# Adding items of different types
y = set([])
y.add(5) # an integer
y.add(8.76) # a real
y.add('abc') # string or symbol
y.add(False) # logical type

The result of the program

Enter number:5
x = {1, 2, 3, 4, 5]}
y = {False, 8.76, 'abc', 5]}

 

13. Function remove(). Removing an item from a set

The remove() function removes an element from the set. The general form of a function declaration

set.remove(elem)

here set is the set to remove the elem element from.

If, when calling a function, the elem element does not fit in the set, then a KeyError exception is raised, which looks something like this

KeyError: elem

Example.

# Sets
# Function remove() - removing an item from a set

x = {1,2,3,4,5}
x.remove(3) # x = {1, 2, 4, 5}
x.remove(4) # x = {1, 2, 5}

 

14. Function discard(). Removing an item from a set without throwing an exception

The discard() function removes an element from the set if the element is in the set. The general form of using the function is as follows

set.discard(elem)

here set is the set from which the elem element is removed. Unlike the remove() function, this function does not generate an error when the elem element is not in the set.

Example.

# Sets
# Function discard() - removing an item from a set

x = {1,2,3,4,5}

x.discard(3) # x = {1,2,4,5}
x.discard(9) # x = {1,2,4,5}
x.discard(1) # x = {2,4,5}

 

15. Function pop(). Pop a random element from a set

The pop() function gets an element from a set. The function returns the given item. General form of using a function

elem = set.pop()

here set is a nonempty set from which the elem element is got. After that, the set set does not contain the elem element.

If the set is empty, then a KeyError exception is thrown. This exception is thrown if a key is not found in the set.

Example.

# Sets
# Function pop() - pop an element from a set

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

elem = x.pop()
print('elem = ', elem)
print('x = ', x)

The result of the program

elem = 1
x = {2, 3, 4, 5, 6, 7, 8, 9}

 

16. Function clear(). Remove all items from the set

The clear() function removes all elements from the set. The general form of using the function is as follows

set.clear()

here set is the set from which the elements are removed.

Example.

# Sets
# Function clear()

x = set('BestProg') # create a set
print('x = ', x) # display on the screen

x.clear() # clear the set
print('x = ', x)

The result of the program

x = {'B', 'e', 'g', 'o', 'P', 's', 'r', 't'}
x = { }

 


Related topics