首页 > 编程语言 > 详细

python中不可变集合

时间:2021-03-03 14:57:26      阅读:18      评论:0      收藏:0      [点我收藏+]

 

1、

>>> a = {1,2,3}
>>> a
{1, 2, 3}
>>> type(a)
<class set>
>>> a.add(4)
>>> a
{1, 2, 3, 4}
>>> b = frozenset({1,2,3})    ## 不可变集合
>>> b
frozenset({1, 2, 3})
>>> type(b)
<class frozenset>
>>> b.add(4)
Traceback (most recent call last):
  File "<pyshell#845>", line 1, in <module>
    b.add(4)
AttributeError: frozenset object has no attribute add
>>> c = frozenset(a)     ## 不可变集合
>>> c
frozenset({1, 2, 3, 4})
>>> c.add(4)
Traceback (most recent call last):
  File "<pyshell#848>", line 1, in <module>
    c.add(4)
AttributeError: frozenset object has no attribute add

 

python中不可变集合

原文:https://www.cnblogs.com/liujiaxin2018/p/14473585.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!