English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutorial básica de Python

Control de flujo de Python

Funciones en Python

Tipos de datos en Python

Operaciones de archivo de Python

Objetos y clases de Python

Fecha y hora de Python

Conocimiento avanzado de Python

Manual de referencia de Python

Uso y ejemplo de métodos de frozenset en Python

Python built-in functions

El método Frozenset() devuelve un objeto frozenset inmutable, inicializado con los elementos del iterable dado.

El conjunto congelado es soloPython setversión inmutable de un objeto. Aunque se puede modificar en cualquier momento los elementos del conjunto, los elementos del conjunto congelado se mantienen inmutables después de su creación.

Por lo tanto, el conjunto congelado se puede utilizar comoEn el diccionario deUna clave o un elemento utilizado como otro conjunto. Sin embargo, al igual que un conjunto, no es ordenado (se puede establecer un elemento en cualquier índice).

The syntax of the Frozenset() method is:

frozenset([iterable])

Frozenset() parameters

The Frozenset() method can optionally use a single parameter:

  • iterable (optional) -iterable, it contains elements used to initialize Frozenset.
    Can set Iterable, Dictionary,Tupleetc.

Frozenset() return value

The Frozenset() method returns an immutable Frozenset (frozen set) initialized with elements from the given iterable.

If no parameters are passed, it returns an empty Frozenset.

Example1How does frozenset() work in Python?

# Tuple vowels
vowels = ('a', 'e', 'i', 'o', 'u')
fSet = frozenset(vowels)
print('The frozen set is:', fSet)
print('The empty frozen set is:', frozenset())

When running the program, the output is:

The frozen set is: frozenset({'o', 'i', 'e', 'u', 'a'})
An empty frozen set is: frozenset()

Example2Dictionary's frozenset()

When you use a dictionary as an iterable object for frozenset. You only need the keys of the dictionary to create a set.

# Random dictionary
person = {'name': 'John', 'age': 23, 'sex': 'male'
fSet = frozenset(person)
print('The frozen set is:', fSet)

When running the program, the output is:

The frozen set is: frozenset({'name', 'sex', 'age'})

Frozenset operations

Like a regular set, frozenset can also perform different operations, such as union, intersection, etc.

Python built-in functions