1. 程式人生 > 其它 >Python frozenset 集合 - Python零基礎入門教程

Python frozenset 集合 - Python零基礎入門教程

目錄

零基礎 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

未經允許不得轉載:猿說程式設計 » Python frozenset 集合

本文由部落格 - 猿說程式設計 猿說程式設計 釋出!