frozenset()
函数返回不能修改的 frozenset 对象。它包含一个无序的对象集合,并且是不可更改的,因此它可以用作字典中的键。
**frozenset([iterable])** #Where iterable can be a list, string, tuple, dictionary , set etc
frozenset()
参数:frozenset()
函数接受一个参数。如果给定了 iterable 参数,它将从中返回一个 frozenset。iterable 包含初始化 frozenset 的元素。
参数 | 描述 | 必需/可选 |
---|---|---|
可迭代的 | 可迭代的可以是集合、字典、元组等。 | 可选择的 |
没有争论 | 空 frozenset 对象 | 可选择的 |
frozenset()
返回值返回值是给定 iterable 的不可变(不能修改)的冻结集。
| 投入 | 返回值 | | 整数 | 整数冻结集 | | 性格;角色;字母 | 冻结字符集 | | 无参数 | 空集 |
frozenset()
方法的示例frozenset()
的工作原理 # tuple of vowels
vowels = ('a', 'e', 'i', 'o', 'u')
fSet = frozenset(vowels)
print('The frozen set is:', fSet)
print('The empty frozen set is:', frozenset())
# frozensets are immutable
fSet.add('v')
输出:
The frozen set is: frozenset({'a', 'o', 'u', 'i', 'e'})
The empty frozen set is: frozenset()
Traceback (most recent call last):
File "<string>, line 8, in <module>fSet.add('v')
AttributeError: 'frozenset' object has no attribute 'add'</module></string>
frozenset()
fs = frozenset([1, 2, 3, 4, 5, 4, 3])
for x in fs:
print(x)
输出:
1
2
3
4
5
frozenset()
# random dictionary pers "John", "age": 23, "sex": "male"}
fSet = frozenset(person)
print('The frozen set is:', fSet)
输出:
The frozen set is: frozenset({'name', 'sex', 'age'})
Frozenset()
处理复制、差、交集、对称差和并集等操作 # Frozensets
# initialize A and B
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
# copying a frozenset
C = A.copy() # Output: frozenset({1, 2, 3, 4})
print(C)
# union
print(A.union(B)) # Output: frozenset({1, 2, 3, 4, 5, 6})
# intersection
print(A.intersection(B)) # Output: frozenset({3, 4})
# difference
print(A.difference(B)) # Output: frozenset({1, 2})
# symmetric_difference
print(A.symmetric_difference(B)) # Output: frozenset({1, 2, 5, 6})
输出:
frozenset({1, 2, 3, 4})
frozenset({1, 2, 3, 4, 5, 6})
frozenset({3, 4})
frozenset({1, 2})
frozenset({1, 2, 5, 6})
Frozenset()
处理像 isdisjoint、issubset 和 issuperset 这样的方法 # Frozensets
# initialize A, B and C
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
C = frozenset([5, 6])
# isdisjoint() method
print(A.isdisjoint(C)) # Output: True
# issubset() method
print(C.issubset(B)) # Output: True
# issuperset() method
print(B.issuperset(C)) # Output: True
输出:
True
True
True
本文链接:http://task.lmcjl.com/news/201.html