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
Set:
mutable
Memory Footprint:
>>> from sys import getsizeof
>>>
>>> a = {1, 2, 3}
>>> b = frozenset({1, 2, 3})
>>>
>>> getsizeof(a)
216
>>>
>>> getsizeof(b)
216
3.4.5. Assignments¶
"""
* Assignment: Sequence Frozenset Create
* 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:
>>> import sys
>>> sys.tracebacklimit = 0
>>> assert result is not Ellipsis, \
'Assignment solution must be in `result` instead of ... (Ellipsis)'
>>> type(result)
<class 'frozenset'>
>>> len(result)
3
>>> 'a' in result
True
>>> 1 in result
True
>>> 2.2 in result
True
"""
# Given
result = ... # frozenset with 'a' and 1 and 2.2
"""
* Assignment: Sequence Frozenset Split
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min
English:
1. Use data from "Given" section (see below)
2. Define `result: frozenset`
3. Split lines and convert result to frozenset
4. Compare result with "Tests" section (see below)
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Zdefiniuj `result: frozenset`
3. Podziel linie i przekonwertuj wynik do frozenset
4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)
Hint:
* `str.splitlines()`
Tests:
>>> import sys
>>> sys.tracebacklimit = 0
>>> assert result is not Ellipsis, \
'Assignment solution must be in `result` instead of ... (Ellipsis)'
>>> type(result)
<class 'frozenset'>
>>> len(result)
3
>>> '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
"""
# Given
DATA = """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."""
result = ... # frozenset with DATA split by lines
"""
* Assignment: Sequence Frozenset Join
* 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)
Hint:
* `str.join()`
Tests:
>>> import sys
>>> sys.tracebacklimit = 0
>>> assert result is not Ellipsis, \
'Assignment solution must be in `result` instead of ... (Ellipsis)'
>>> type(result)
<class 'str'>
>>> len(result)
150
>>> result.count('\\n')
2
>>> '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
"""
# 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.'})
result = ... # frozenset with lines from DATA joined with newline (`\n`) character