Python frozenset 集合

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门

       在前一篇文章中我们对 Python set 集合 做了详细的讲解,而本文讲解的 frozenset 集合 其实和 set 集合类似!

       与 Python set 集合区别在于 frozenset 集合不能修改/添加/删除,其他功能和set集合一样,这就有点类似列表 list元组 tuple 的区别


一.Python frozenset 集合语法

# 创建一个frozenset集合
a = frozenset(iterable)

       其中 iterable 是序列或者可迭代对象,并返回 frozenset 集合


二.Python frozenset 集合使用

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python frozenset 集合.py
@Time:2021/04/04 11:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""


a = frozenset(["q123","python","frozenset"])
print(a)
# 获取a的类型
print(type(a))
# 修改frozenset集合数据,程序报错:AttributeError: 'frozenset' object has no attribute 'add'
# a.add("hello")

'''
输出结果:

frozenset({'frozenset', 'python', 'q123'})
<class 'frozenset'>
'''

       在上面代码中,如果尝试修改 frozenset 集合的数据,即使用 add 添加数据,程序报错:AttributeError: ‘frozenset’ object has no attribute ‘add’!

       原因:frozenset 集合不能修改/添加/删除,其他功能和set集合一样


三.猜你喜欢

  1. Python 字符串/列表/元组/字典之间的相互转换
  2. Python 局部变量和全局变量
  3. Python type 函数和 isinstance 函数区别
  4. Python is 和 == 区别
  5. Python 可变数据类型和不可变数据类型
  6. Python 浅拷贝和深拷贝
  7. Python 递归函数
  8. Python sys模块
  9. Python 列表list
  10. Python 元组tuple
  11. Python 字典 dict
  12. Python 条件推导式
  13. Python 列表推导式
  14. Python 字典推导式
  15. Python 函数声明和调用
  16. Python 不定长参数 *argc/**kargcs

ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞2 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容