What is Frozenset in Python

set frozenset

introduction

The sets in python (set objects) were modeled on those used in mathematics.
But what is special about quantities?
Sets are DISORDERED collections of objects that appear only once in the set object.
So you cannot rely on an index to point to a certain value at a certain position in the network (as with st, list, tuple), you still have one key, like one dictionary.

And if you pass an ordered list (set (ordered_list)), there is no guarantee that the order will be maintained. The (operating) system decides how and where the data should be located in the RAM, so this can also provide different outputs from system to system. So the order is at disordered Data types are never a criterion.

There are two types of objects in python:

objects of type set are mutable, which is not the case with frozenset objects.
Sets can be converted into a frozenset in no time and vice versa.

Which set operations are there?

  • x is an element of the set S
  • x is not an element of S
  • S1 is equal to S2
  • S1 is not equal to S2
  • S1 is *real* Subset of S2
  • S1 is *fake* Subset of S2
  • S1 is *real* superset from S2
  • S1 is *fake* superset from S2
  • Average of S1 and S2
  • S1 unites with S2
  • Difference between S2 and S1
  • Symmetrical difference between S2 and S1

These set operations can also be performed on d.keys ()- and d.items ()Objects (d is a dict object) can be applied!

set

Immediately in-medias-res

>>> x = (1,1,2,2,3,3,4,5,6) >>> y = set (x) >>> print (y) {1, 2, 3, 4, 5 , 6} >>> print (type (y)) >>> S4 = set () >>> S3 = frozenset () >>> print (type (S3)) >>>

this is how you build a set; more samples

>>> a = {1, 2, 3, 2, 3} >>> print (a) {1, 2, 3} >>> type (a) >>>

We could also use the functions set() and frozenset ()to create empty set objects because it still holds
results in an empty dictionary object!

A set may / can only contain unchangeable objects!
So...

  • not a list object
  • no dict object

In addition, nesting of sets is not possible!

>>> quantity = {2, [1,2], 'a'} Traceback (most recent call last): File "", line 1, in TypeError: unhashable type: 'list' >> >

Let's look at the following prime example, where a check is made to see how often an item occurs in a sequence.

>>> wort = 'Donaudampfschifffahrtsgesellschaft' >>> for c in sorted (list (set (word))): ... print ('{}: {} x'.format (c, wort.count (c)) ) ... a: 4x c: 2x d: 2x e: 2x f: 5x g: 1x h: 3x i: 1x l: 2x m: 1x n: 1x o: 1x p: 1x r: 1x s: 4x t : 2x u: 1x >>>

It continues with various operations.

>>> m1 = set (range (10)) >>> m2 = set (range (5,15)) >>> print ('m1 combined with m2 = {}'. format (m1 | m2)) m1 combined with m2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14} >>> print ('m1 combined with m2 = {}'. format (m1.union (m2))) m1 united with m2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14} >>> print ( 'Average of m1 and m2 = {}'. Format (m1 & m2)) Average of m1 and m2 = {8, 9, 5, 6, 7} >>> print ('Average of m1 and m2 = {}' .format (m1.intersection (m2))) Average of m1 and m2 = {8, 9, 5, 6, 7} >>> print ('The elements {} of m1 are not in m2'.format (m1 - m2)) The elements {0, 1, 2, 3, 4} of m1 are not in m2 >>> print ('The elements {} of m2 are not in m1'.format (m2.difference (m1))) The elements {10, 11, 12, 13, 14} of m2 are not in m1 >>>

That reminds me a lot of the set theory of the first grade in grammar school.
| so is the same union() and & Average (s1.intersection (s2).
Let's take a look at other set methods using the following examples.

>>> m3 = set (range (5,10)) >>> m3.issubset (m1) True >>> m2.issuperset (m3) True >>> m3.clear () >>> m3.add (' was empty ') >>> print (m3) {' was empty '} >>> m3.add (' bins never ') >>> m4 = m3.copy () >>> print (m4) {' bins never ',' was empty '} >>>' never 'in m4 False >>>' never 'in m4 [0] Traceback (most recent call last): File "", line 1, in TypeError : 'set' object does not support indexing >>>

clear that this is not possible - the set object is a DISORDERED type -> no index query (m4 [0]) makes sense (who knows / cares what is on position 0!)

>>> m5 = m3 >>> print ('id-m3: {0} \ nid-m4: {1} \ nid-m5: {2}'. format (id (m3), id (m4), id (m5))) id-m3: 3083592508 id-m4: 3083594300 id-m5: 3083592508 >>>

The copy() Method of set actually has its own set object (its own id) built.
Using the discard() Method we can remove an item from a set (if the object specification that is to be removed from the set object is not included in the set object, there is no noise / error / exception) - we will afterwards nor the remove() Method and the pop() Method that returns an object from the set object to catch pops out of the set.
It seems that pop () pops out the first item from the set object, but since there is no Positions there, it is impossible to from position to speak.

>>> m5.discard ('mi is net in m5') >>> print (m5) {'bins never', 'was empty'} >>> m5.discard ('bins never') >>> print ( m3) {'was empty'} >>> m5 = m1.copy () >>> m5.remove (5) >>> m5.remove ('a') Traceback (most recent call last): File " ", line 1, in KeyError: 'a' >>> m5.pop () 0 >>> m5.pop () 1 >>> print (m5) {2, 3, 4, 6, 7 , 8, 9} >>>

On the subject vs (subset)

>>> quantity1 = {0,1,2,3} >>> quantity2 = {1,2} >>> quantity3 = set (range (4)) >>> quantity2 <= quantity1 True >>>

Quantity2 is a subset of Quantity1.
is it a real or a spurious subset of set1?

>>> quantity2 >>

Quantity2 IS REAL Subset of set 1

>>> quantity3 <= quantity1 True >>> quantity3 >> quantity3.issubset (quantity1) True >>>

Set3 is also a subset of Set1, but a FAKE.

symmetric_difference ()

m1.symmetric_difference (m2) can also be written like this:

and that does it:
unites difference m1-m2 with m2-m1
(m1 - m2) | (m2 - m1)
so

>>> print (m1) {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} >>> print (m2) {5, 6, 7, 8, 9, 10, 11, 12, 13, 14} >>> print ('(m1 - m2) | (m2 - m1) = {}'. Format (m1.symmetric_difference (m2))) (m1 - m2) | (m2 - m1) = {0, 1, 2, 3, 4, 10, 11, 12, 13, 14} >>> print ('(m1 - m2) | (m2 - m1) = {}'. format (m1 ^ m2)) (m1 - m2) | (m2 - m1) = {0, 1, 2, 3, 4, 10, 11, 12, 13, 14} >>>

and that was achieved in the following way

>>> m1.union (m2) - m1.intersection (m2) {0, 1, 2, 3, 4, 10, 11, 12, 13, 14} >>>

the built-in functions len(), Max() or min() (and probably others) also work for (frozen) set objects!

>>> print (max (m2)) 14 >>> print (len (m1.symmetric_difference (m2))) 10 >>>

Sometimes you wonder why there are the set methods on the one hand and the characters like on the other
gives?
with m1 & m2 m1, m2 MUST be set / frozenset objects, while with set / frozenset methods the transferred objects MUST be only. iterable need to be
SETOBJECT.symmetric_difference (STRING)
Example:

>>> print (word) 'donaudampfschifffahrtsgesellschaft' >>> wort2 = 'superkalefragelisticxpigaligetic' >>> print ('The "symmetrical difference" of \ n {} \ nand \ n {} \ nis \ n {}'. format ( wort, wort2, set (wort) .symmetric_difference (wort2))) The "symmetric difference" between the Danube steamship company and the super calefragelistic experimental is {'d', 'k', 'm', 'o', 'n', 'x'} >>>

set comprehension

We already know list comprehension and dict comprehension.
And so set comprehensions are also a convenient way of creating a set object.
The similarity to dict-comprehensions is great, so be careful!

>>> s = {str (v) for v in range (1,15) if v% 2} >>> print (type (s)) >>> print (s) {'11 ',' 13 ',' 1 ',' 3 ',' 5 ',' 7 ',' 9 '} >>>

You can see again how messy the items are in the set object.

frozenset

frozenset objects can NOT be changed (immuteable).
Frozensets objects can therefore also be used as a key in a dictionary or be an element of a set object.
Any methods that did not change the set object also apply to frozensets.
Frozen sets are created as follows

>>> m6 = frozenset ('michael') >>> m7 = frozenset ('nicholas') >>> print (type (m6)) >>> print (m6.intersection (m7)) frozenset ({'a', 'i', 'c', 'l', 'h'}) >>>

What if I frozenset with set.

>>> wasbinich = m1.union (m7) >>> print (type (wasbinich)) >>>

So a set object comes out.

>>> s_name = set (m7) >>> print (type (s_name)) >>> f_name = frozenset (s_name) >>> print (type (f_name)) >>>

so you can effortlessly turn a set into a frozenset object and vice versa.


Here it goes to the top of the page and there it goes to the python start page