怎么使用frozenset
函数作用
frozenset() 返回一个冻结的集合,冻结后集合不能再添加或删除任何元素。与之对应的是set函数,set无序排序且不重复,是可变的,有add(),remove()等方法。
函数原型
1 | frozenset ([iterable]) |
版本兼容性
Python3.x
>= Python2.4
函数参数
iterable -- 可迭代的对象,比如列表、字典、元组、字符串等等。
函数用法
根据参数给定的对象, 返回相应的不可变集合。
返回值
返回新的 frozenset 对象,如果不提供任何参数,默认会生成空集合。
英文解释
Return a new frozenset object, optionally with elements taken from iterable. frozenset is a built-in class. See frozenset and Set Types — set, frozenset for documentation about this class.
For other containers see the built-in set, list, tuple, and dict classes, as well as the collections module.
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | >>> num = frozenset ( range ( 10 )) # 创建不可变集合 >>> num frozenset ([ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]) >>> sitename = frozenset ( 'pythontab' ) >>> sitename frozenset ([ 'a' , 'b' , 'h' , 'o' , 'n' , 'p' , 't' , 'y' ]) # 创建不可变集合, 注意:顺序不是按照参数的顺序 >>> >>> sitename2 = set ( 'pythontab' ) >>> sitename2 set ([ 'a' , 'b' , 'h' , 'o' , 'n' , 'p' , 't' , 'y' ]) #可变集合 >>> >>> sitename2.add( '.com' ) #向可变集合添加成员 >>> sitename2 set ([ 'a' , 'b' , 'h' , 'o' , 'n' , 'p' , 't' , 'y' , '.com' ]) >>> >>> sitename.add( '.com' ) #向不可变集合添加成员, 会报错 Traceback (most recent call last): File "<stdin>" , line 1 , in <module> AttributeError: 'frozenset' object has no attribute 'add' >>> |
本文由 帝一博客 原创发布。用户在本站发布的原创内容(包括但不仅限于回答、文章和评论),著作权均归用户本人所有。独家文章转载,请联系邮箱:17762131@qq.com。获得授权后,须注明本文地址: https://bubukou.com/pythonpachong/1079.html
-
【原创】python_docx制作word文档实例demo
2019-10-29 16:15
-
python遍历字典dict
2019-09-22 12:05
-
python 两个list合并成一个dict的方法
2019-09-22 11:56
-
【原创】python 爬取wowo扣扣爬虫代码
2019-09-05 14:46
-
windows 怎么安装 python 的 win32 wi...
2019-08-10 18:15
-
[原创]python 证书验证http 304问题
2019-08-07 14:02
-
【原创】python 爬取小清新唯美图片
2019-07-18 22:19
-
Python简单计算文件MD5值的方法示例
2019-07-18 22:13
-
从零开始Python对redis作为消息队列的使用
2019-07-10 08:37
-
如何安装python到setuptools、pip工具安装
2019-07-10 07:52
网友留言评论