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集合


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章