目錄
- 一.Python frozenset 集合語法
- 二.Python frozenset 集合使用
- 三.猜你喜歡
零基礎 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 集合一樣!
三.猜你喜歡
- Python 字串/串列/元組/字典之間的相互轉換
- Python 區域變數和全域變數
- Python type 函式和 isinstance 函式區別
- Python is 和 == 區別
- Python 可變資料型別和不可變資料型別
- Python 淺拷貝和深拷貝
- Python 遞回函式
- Python sys 模塊
- Python 串列 list
- Python 元組 tuple
- Python 字典 dict
- Python 條件推導式
- Python 串列推導式
- Python 字典推導式
- Python 函式宣告和呼叫
- Python 不定長引數 *argc/**kargcs
未經允許不得轉載:猿說編程 ? Python frozenset 集合
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/288120.html
標籤:Python
