Python. Limitations on the use of sets. The use of class frozenset. Generators of sets.




Limitations on the use of sets. The use of class frozenset. Generators of sets.


Contents


Search other websites:

1. Limitations on the use of sets. What types are forbidden to use in sets?

It is forbidden to include objects of mutable types in sets, namely:

  • lists;
  • dictionaries;
  • mutable sets that were created by the constructor of the set class or by an assignment operation.

If you try to include a mutable type in the set, the interpreter will throw an error

TypeError: unhashable type: 'set'

 

2. What types are allowed to be included in sets?

It is permitted to include objects of the so-called immutable or hashable types into sets. An object is considered “hashable” if it has a value that never changes throughout the life of this object in the program.

Hashable types whose objects can be included in sets include:

  • numeric types (real, integer types);
  • logical type;
  • string type;
  • tuple;
  • immutable set that was created using the constructor of the frozenset class.

 

3. Features of using the frozenset class for sets. Example

The frozenset class is designed to create an immutable set. You cannot add new elements to an immutable set or modify it in any way. An immutable set can be included as an element (component) of another set. For an immutable set, you cannot apply functions that change it:

  • update() – union of the current set with the other;
  • intersection_update() – intersection of the current set with others;
  • difference_update() – the difference of the current set with others;
  • symmetric_difference_update() – the symmetric difference of the current set with another set;
  • add() – adding an element to the current set;
  • remove() – removing an element from the current set;
  • discard() – removing an element from the current set, if it is in it;
  • pop() – pulling element from the current set;
  • clear() – removal of all elements from the set.

More details about the operation of these functions is described in the topic:

Example

# Sets
# Function frozenset() - providing a set of immutable type
x = frozenset([1, 2, 3, 4]) # x - immutable set
y = set([5,6,7]) # y - mutable set
z = { 8, 9, x } # the set x includes the set z, the set z is mutable

print('x = ', x)
print('y = ', y)
print('z = ', z)

The result of the program

x = frozenset({1, 2, 3, 4}) 
y = {5, 6, 7} 
z = {8, 9, frozenset({1, 2, 3, 4})}


 

4. Generators of sets. Concept. General form

A generator of sets is a special construction that is designed to create a set using the for loop. The general form of the set generator

{ statement for item in [items]}

where

  • statement – an expression (formula) forming the elements of the newly created set based on the list of items;
  • item – element of the set that is used in the statement. This element takes a value from a set of items;
  • items – a set of elements of the original set, which are separated by commas. The set of items are direct data that are used in the statement to form a new set.

 

5. Examples of generators of sets

Example 1. Integer numbers.

# Generator of sets
# Form a set of integers
y = { 3*x for x in set([1,2,3,4,5,6])} # y = {3, 6, 9, 12, 15, 18}
print('y = ', y)

z = { t*t for t in { 2, 4, 6, 8 }}
print('z = ', z)

The result of the program

y = {3, 6, 9, 12, 15, 18}
z = {64, 16, 4, 36}

Example 2. Real numbers

# Generator of sets
# Form a set of real numbers
y = { x+0.5 for x in set([1.5, 2.5, 3.5, -8.0])} # y = {-7.5, 2.0, 3.0, 4.0}
print('y = ', y)

z = { t + 2*t for t in { 0.5, 0.75, 1.2, -3.4 }}
print('z = ', z) # z = {3.5999999999999996, 1.5, 2.25, -10.2}

The result of the program

y = {-7.5, 2.0, 3.0, 4.0}
z =   {3.5999999999999996, 1.5, 2.25, -10.2}

Example 3. Strings.

# Generator of sets
# Form a set using strings
x = { s for s in set('abcdef')} # x = {'a', 'd', 'c', 'f', 'e', 'b'}
print('x = ', x)

y = { s*3 for s in set('1DF')}
print('y = ', y)

z = { '_'+s for s in {'j', 'k', 'l'}}
print('z = ', z)

The result of the program

x = {'b', 'c', 'f', 'd', 'e', 'a'}
y = {'FFF', '111', 'DDD'}
z = {'_l', '_k', '_j'}

 


Related topics