SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 

    在前一篇文章中我们对 python set集合 做了详细的讲解,而本文讲解的 frozenset集合 其实和set集合类似!区别在于frozenset集合不能修改/添加/删除,其他功能和set集合一样,这就有点类似列表list和元组tuple的区别。

 

python frozenset集合(17) Python 第1张

 

一.frozenset集合语法

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

 

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

 

二.frozenset集合使用

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com
 
@File:python_frozenset.py
@Time:2019/11/10 21:25
 
@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’!

 

python frozenset集合(17) Python 第2张

 

猜你喜欢:

    1.python set集合

    2.python threading线程创建和参数传递

    3.python threading线程互斥锁Lock

    4.python threading线程定时器Timer

 

    转载请注明:猿说Python » python frozenset集合

 

技术交流、商务合作请直接联系博主 扫码或搜索:猿说python python frozenset集合(17) Python 第3张 猿说python 微信公众号 扫一扫关注
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄