python frozenset集合

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

 

哟呵

 

一.frozenset集合语法


1

2

# 创建一个frozenset集合

a = frozenset(iterable)

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

 

二.frozenset集合使用


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

# !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")

输出结果:

1

2

frozenset({'frozenset', 'python', 'q123'})

<class 'frozenset'>

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

 

python

 

猜你喜欢:

1.python set集合

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

3.python threading线程互斥锁Lock

4.python threading线程定时器Timer

 

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


猜你喜欢

转载自blog.51cto.com/14531342/2475830