Python frozenset
Un frozenset este o mulțime imuabilă: nu poate fi modificată niciodată.
Elementele sale sunt unice, neordonate și nemodificabile, ca într-o mulțime obișnuită.
Dar nu poți niciodată să le adaugi sau să le elimini.
Crearea unui frozenset
Transmite orice obiect iterabil constructorului frozenset() ca să obții un frozenset.
Exemplu
Creează un frozenset și verifică-i tipul:
Prompt
Scrie un program Python care demonstrează codul prezentat în exemplul PromptCookbook.
x = frozenset({"apple", "banana", "cherry"})
print(x)
print(type(x))
Ieșire: program Python complet
main.py
x = frozenset({"apple", "banana", "cherry"})
print(x)
print(type(x))
Metodele frozenset
Așadar, adăugarea și eliminarea elementelor nu sunt posibile. Totuși, orice operație a mulțimilor care lasă datele neatinse funcționează în continuare.
main.py
# Frozenset Methods
# Method Shortcut Description
# copy() Returns a shallow copy
# difference() - Returns a new frozenset with the difference
# intersection() & Returns a new frozenset with the intersection
# isdisjoint() Returns True if there is NO intersection between two frozensets
# issubset() <= / < Returns True if this frozenset is a (proper) subset of another
# issuperset() >= / > Returns True if this frozenset is a (proper) superset of another
# symmetric_difference() ^ Returns a new frozenset with the symmetric differences
# union() | Returns a new frozenset containing the union