*Memo:
- My post explains a frozenset (2).
- My post explains a frozenset (3).
- My post explains a frozenset (4).
- My post explains a set (1).
- My post explains set and frozenset functions (1).
- My post explains 10 collection types and their related posts.
A frozenset:
- is the unordered immutable(hashable) collection of zero or more elements whose type is
frozenset:- Unordered means that the order of each element in a frozenset isn't kept so it doesn't guarantee that the order is always the same.
- Immutable(Hashable) means the elements of a frozenset cannot be changed.
- doesn't allow duplicated elements (even with different types).
- can have the hashable types of elements:
- A hashable type is the type whose value cannot be changed like
str,bytes,int,float,complex,bool,tuple,frozenset,rangeoriterator.
- A hashable type is the type whose value cannot be changed like
- cannot have the unhashable types of elements:
- A unhashable type is the type whose value can be changed like
bytearray,list,setordict.
- A unhashable type is the type whose value can be changed like
- can be used with len() to get the length.
- is
Trueif it's non-empty andFalseif it's empty, checking it with bool(). - is
Falseif it's non-empty andTrueif it's empty, inverting the truth value withnot. - can be checked if a specific element is and isn't in the frozenset with
inandnot inrespectively. - can be checked if the frozenset is and isn't referred to by two variables with
isandis notrespectively. - and other frozenset can be checked if all the elements in:
- them are and aren't equal with
==and!=respectively:-
==and!=can also check if the types of two values are and aren't the same respectively.- A frozenset and set and vice versa are
Trueeven though their types are different.
- A frozenset and set and vice versa are
-
- the frozenset are in other frozenset with
<=. - the frozenset and other elements are in other frozenset with
<. - other frozenset are in the frozenset with
>=. - other frozenset and other elements are in the frozenset with
>.
- them are and aren't equal with
- and other frozenset can be checked if they have and don't have their common elements with
bool()and&and withnotand&respectively. - cannot be enlarged with
*and a number. - and other frozensets cannot be concatenated with
+. - and other frozenset can return:
- all the elements in them with
'|'(Union: A ∪ B). - their common elements with
'&'(Intersection: A ∩ B). - the elements in the frozenset which aren't in other frozenset with
'-'(Difference: A - B). - the elements in either the frozenset or other frozenset but not both with
'^'(Symmetric Difference: A Δ B).
- all the elements in them with
- can be iterated with a
forstatement. - can be unpacked with an assignment and
forstatement, function and*but not with**. - can be created by frozenset() with or without an iterable and by a frozenset comprehension:
- For
frozenset(), the words type conversion are also suitable in addition to the word creation.
- For
- cannot be big because it gets
MemoryError. - cannot be read by indexing and slicing.
- cannot be changed by indexing, slicing and a del statement.
- can be continuously used through multiple variables.
- cannot be shallow-copied by frozenset.copy(), copy.copy() and
frozenset(). - can be deep-copied by copy.deepcopy().
Be careful, a big frozenset gets MemoryError.
MemoryError.frozenset() can create a frozenset as shown below:
A = frozenset() # Empty 1D frozenset
A = frozenset([10, 20, 30, 40, 50]) # 1D frozenset
A = frozenset([10, 20, 30, 10, 20, 30]) # 1D frozenset
A = frozenset([10, 20, 30, 40, # 2D frozenset
frozenset([50, 60, 70, 80])])
A = frozenset([frozenset([10, 20, 30, 40]), # 2D frozenset
frozenset([50, 60, 70, 80])])
A = frozenset([frozenset([10, 20, 30, 40]), # 3D frozenset
frozenset([frozenset([50, 60]),
frozenset([70, 80])])])
A = frozenset([frozenset([frozenset([10, 20]), # 3D frozenset
frozenset([30, 40])]),
frozenset([frozenset([50, 60]),
frozenset([70, 80])])])
# No error
print(frozenset([0, 0.0, 0.0+0.0j, False]))
print(frozenset([1, 1.0, 1.0+0.0j, True]))
print(frozenset(['A', b'A', 2, 2.3, 2.3+4.5j, True, (2, 3),
frozenset([2, 3]), range(2, 3), iter([2, 3])]))
print(len(frozenset([10, 20, 30, 40, 50])))
print(bool(frozenset([0])))
print(bool(frozenset([frozenset()])))
print(bool(frozenset()))
print(not frozenset([0]))
print(not frozenset([frozenset()]))
print(not frozenset())
print(10 in frozenset([10, frozenset([20, 30])]))
print(10 not in frozenset([10, frozenset([20, 30])]))
print(frozenset([10, 20, 30]) is frozenset([10, 20, 30]))
print(frozenset([10, 20, 30]) is not frozenset([10, 20, 30]))
print(frozenset([10, 20, 30]) == frozenset([10, 20, 30]))
print(frozenset([10, 20, 30]) != frozenset([10, 20, 30]))
print(frozenset([10, 20, 30]) <= frozenset([10, 20, 30]))
print(frozenset([10, 20, 30]) >= frozenset([10, 20, 30]))
print(frozenset([10, 20, 30]) < frozenset([10, 20, 30]))
print(frozenset([10, 20, 30]) > frozenset([10, 20, 30]))
print(bool(frozenset([10, 20, 30]) & frozenset([20, 40])))
print(not (frozenset([10, 20, 30]) & frozenset([20, 40])))
print(frozenset([10, 50]) | frozenset([10, 30, 50]))
print(frozenset([10, 20, 30, 40]) & frozenset([10, 30, 50]))
print(frozenset([10, 20, 30, 40]) - frozenset([10, 30, 50]))
print(frozenset([10, 20, 30, 40]) ^ frozenset([10, 30, 50]))
for x in frozenset([0, 1, 2, 3, 4]): print(x)
for x in frozenset([frozenset([10, 20, 30, 40]),
frozenset([50, 60, 70, 80])]: print(x)
for x in frozenset([frozenset([frozenset([10, 20]),
frozenset([30, 40])]),
frozenset([frozenset([50, 60]),
frozenset([70, 80])])]): print(x)
v1, v2, v3 = frozenset([0, 1, 2]); print(v1, v2, v3)
v1, *v2, v3 = frozenset([0, 1, 2, 3, 4, 5]); print(v1, v2, v3)
for v1, v2, v3 in frozenset([frozenset([0, 1, 2]),
frozenset([3, 4, 5])]): print(v1, v2, v3)
for v1, *v2, v3 in frozenset([frozenset([0, 1, 2, 3, 4, 5]),
frozenset([6, 7, 8, 9, 10, 11])]):
print(v1, v2, v3)
print(*frozenset([0, 1]), 2, *frozenset([3, 4, *frozenset([5])]))
print(frozenset([*frozenset([0, 1]), 2,
*frozenset([3, 4, *frozenset([5])])]))
print(frozenset(x**2 for x in frozenset([0, 1, 2, 3, 4, 5, 6, 7])))
print(frozenset(frozenset(y**2 for y in x) for x in
frozenset([frozenset([0, 1, 2, 3]),
frozenset([4, 5, 6, 7])])))
print(frozenset(frozenset(frozenset(z**2 for z in y) for y in x) for x in
frozenset([frozenset([frozenset([0, 1]), frozenset([2, 3])]),
frozenset([frozenset([4, 5]), frozenset([6, 7])])])))
# No error
print(frozenset([10, 20, [30, 40], 50, 60]))
print(frozenset([10, 20, {30, 40}, 50, 60]))
print(frozenset([10, 20, {30:40, 50:60}, 70, 80]))
print(frozenset([bytearray(b'Hello')]))
print(frozenset([10, 20, 30, 40, 50]) * 3)
print(frozenset([10, 20, 30]) + frozenset([30, 40]) \
+ frozenset([50, 60, 70, 80]))
print(frozenset(range(1000000000)))
print(frozenset([x for x in range(1000000000)]))
# Error
A frozenset is the unordered immutable(hashable) collection of zero or more elements whose type is frozenset as shown below:
A = frozenset([10, 20, 30, 40, 50])
print(A)
# frozenset({40, 10, 50, 20, 30})
print(type(A))
# <class 'frozenset'>
A[1] = 'X'
A[3] = 'Y'
# TypeError: 'frozenset' object does not support item assignment
A = frozenset() # Empty frozenset
print(A)
# frozenset()
A frozenset doesn't allow duplicated elements (even with different types) as shown below:
A = frozenset([10, 20, 30, 10, 20, 30])
print(A)
# frozenset({10, 20, 30})
A = frozenset([0, 0.0, 0.0+0.0j, False])
print(A)
# frozenset({0})
A = frozenset([1, 1.0, 1.0+0.0j, True])
print(A)
# frozenset({1})
A frozenset can have the hashable types of elements as shown below:
A = frozenset(['A', b'A', 2, 2.3, 2.3+4.5j, True, (2, 3),
frozenset([2, 3]), range(2, 3), iter([2, 3])])
print(A)
# frozenset({True, 2, 2.3, frozenset({2, 3}),
# range(2, 3), <list_iterator object at 0x00000245108C9FF0>,
# (2.3+4.5j), (2, 3), 'A', b'A'})
A frozenset cannot have the unhashable types of elements as shown below:
A = frozenset([10, 20, [30, 40], 50, 60]) # frozenset(list)
# TypeError: unhashable type: 'list'
A = frozenset([10, 20, {30, 40}, 50, 60]) # frozenset(set)
# TypeError: unhashable type: 'set'
A = frozenset([10, 20, {30:40, 50:60}, 70, 80]) # frozenset(dict)
# TypeError: unhashable type: 'dict'
A = frozenset([bytearray(b'Hello')]) # frozenset(bytearray)
# TypeError: unhashable type: 'bytearray'
A frozenset can be used with len() to get the length as shown below:
A = frozenset([10, 20, 30, 40, 50])
print(len(A))
# 5
Top comments (0)