3.4. Sequence Frozenset

3.4.1. Rationale

  • Only unique values

  • Immutable - cannot add, modify or remove items

  • Can store elements of any hashable types

  • Has all set methods such as .intersect(), .subset() .union(), etc.

  • One solid block in memory

  • Frozenset is unordered data structure and do not record element position

  • Do not support getitem and slice

3.4.2. Definition

Defining only with frozenset() - no short syntax:

>>> data = frozenset()

Comma after last element of a one element frozenset is optional:

>>> data = frozenset({1})
>>> data = frozenset({1,})

Brackets inside are required:

>>> data = frozenset({1})
>>> data = frozenset({1, 2, 3})
>>> data = frozenset({1.1, 2.2, 3.3})
>>> data = frozenset({True, False})
>>> data = frozenset({'a', 'b', 'c'})
>>> data = frozenset({'a', 1, 2.2, True, None})

3.4.3. Type Casting

Builtin function frozenset() converts argument to frozenset

>>> data = 'abcd'
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = ['a', 'b', 'c', 'd']
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = ('a', 'b', 'c', 'd')
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = {'a', 'b', 'c', 'd'}
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = frozenset({'a', 'b', 'c', 'd'})
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = [1, 2, 3, [4, 5]]
>>> frozenset(data)
Traceback (most recent call last):
TypeError: unhashable type: 'list'
>>> data = [1, 2, 3, (4, 5)]
>>> frozenset(data) == frozenset({(4, 5), 1, 2, 3})
True

3.4.4. Frozenset vs. Set

Both:

  • unordered

  • impossible to getitem and slice

  • unique elements

  • only hashable elements

Frozenset:

  • immutable

  • one contingent block of data in memory

Set:

  • mutable

  • implemented in memory as list of pointers to objects

  • objects are scattered in memory

3.4.5. Assignments

Code 3.9. Solution
"""
* Assignment: Sequence Frozenset Create
* Filename: sequence_frozenset_create.py
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Create frozenset `result` with elements:
        a. `'a'`
        b. `1`
        c. `2.2`
    2. Compare result with "Tests" section (see below)

Polish:
    1. Stwórz frozenset `result` z elementami:
        a. `'a'`
        b. `1`
        c. `2.2`
    2. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Tests:
    >>> type(result)
    <class 'frozenset'>
    >>> len(result)
    3
    >>> 'a' in result
    True
    >>> 1 in result
    True
    >>> 2.2 in result
    True
"""

Code 3.10. Solution
"""
* Assignment: Sequence Frozenset Newline
* Filename: sequence_frozenset_newline.py
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. Use data from "Given" section (see below)
    2. Define `result: str`
    3. Use `str.join()` to join lines of text with newline (`\n`) character
    4. Compare result with "Tests" section (see below)

Polish:
    1. Użyj danych z sekcji "Given" (patrz poniżej)
    2. Zdefiniuj `result: str`
    3. Użyj `str.join()` aby połączyć linie tekstu znakiem końca linii (`\n`)
    4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Tests:
    >>> type(result)
    <class 'str'>
    >>> len(result)
    150
    >>> 'We choose to go to the Moon.' in result
    True
    >>> 'We choose to go to the Moon in this decade and do the other things.' in result
    True
    >>> 'Not because they are easy, but because they are hard.' in result
    True
    >>> result.count('\\n')
    2
"""


# Given
DATA = frozenset({
    'We choose to go to the Moon.',
    'We choose to go to the Moon in this decade and do the other things.',
    'Not because they are easy, but because they are hard.'})