
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策
- Python - If 語句
- Python - If else
- Python - 巢狀 If
- Python - Match-Case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - While 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式 & 模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅限關鍵字引數
- Python - 位置引數
- Python - 僅限位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類 & 物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝類
- Python - 列舉
- Python - 反射
- Python 錯誤 & 異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 加入執行緒
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路程式設計
- Python - 網路程式設計
- Python - Socket 程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期 & 時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUI
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python collections.ChainMap
在 Python 中,ChainMap 類用於將多個字典或對映轉換為單個單元。如果未指定任何對映,則提供單個空字典,以便新的鏈始終至少具有一個對映。
ChainMap() 類在 Collections 模組中定義。它通常比建立新字典快得多。
語法
以下是 Python ChainMap() 的語法:
collections.ChainMap(iterable1, iterable2, iterable3)
引數
此資料型別接受迭代器作為引數。
返回值
此資料型別返回一個新的 ChainMap 物件,該物件對映多個可迭代物件。
示例
以下是 Python ChainMap() 的基本示例:
# Python program to demonstrate # ChainMap from collections import ChainMap d1 = {'one': 1, 'two': 2} d2 = {'three': 3, 'four': 4} d3 = {'five': 5, 'six': 6} # Defining the chainmap c = ChainMap(d1, d2, d3) print(c)
以下是上述程式碼的輸出:
ChainMap({'one': 1, 'two': 2}, {'three': 3, 'four': 4}, {'five': 5, 'six': 6})
使用 ChainMap 模擬巢狀作用域
ChainMap() 類可用於模擬巢狀作用域,在模板中很有用。它允許我們將多個字典或對映組合成一個單元,因此我們可以像它們是一個一樣在所有字典中搜索元素。ChainMap 會按優先順序順序檢查每個字典,並在找到匹配項後立即停止,而不是單獨搜尋每個字典。
示例
在以下示例中,ChainMap() 與巢狀作用域一起使用:
from collections import ChainMap default_settings = {'theme': 'dark', 'font': 'Arial', 'size': 12} user_settings = {'theme': 'light', 'size': 14} # ChainMap gives priority to the first dictionary (user_settings) settings = ChainMap(user_settings, default_settings) print(settings['theme']) print(settings['font'])
以下是上述程式碼的輸出:
light Arial
ChainMap 中的基礎對映
在 ChainMap 類中,基礎對映(字典)儲存為列表。此列表可透過 maps 屬性訪問,該屬性允許您直接檢視或修改 ChainMap 管理的字典。
示例
這是一個在 ChainMap() 中使用基礎對映的示例:
from collections import ChainMap # Two dictionaries dict1 = {'a': 1, 'b': 2} dict2 = {'b': 3, 'c': 4} # Create a ChainMap combined = ChainMap(dict1, dict2) print(combined['b']) # Accessing the maps attribute print(combined.maps) # Updating the order of dictionaries in the ChainMap combined.maps = [dict2, dict1] print(combined['b'])
以下是上述程式碼的輸出:
2 [{'a': 1, 'b': 2}, {'b': 3, 'c': 4}] 3
使用 ChainMap 合併多個列表
ChainMap() 還可以用於將多個列表轉換為單個單元。
示例
以下是使用 ChainMap() 與列表的示例:
from collections import ChainMap list1 = ['a','b','c','d'] list2 = [1, 2, 3, 4] list3 = ['one','two','three'] chain_list = ChainMap(list1, list2, list3) print(chain_list)
以下是上述程式碼的輸出:
ChainMap(['a', 'b', 'c', 'd'], [1, 2, 3, 4], ['one', 'two', 'three'])
ChainMap 中的訪問操作
我們可以使用 keys() 函式訪問 ChainMap 中所有字典的所有鍵。values() 函式返回 ChainMap 中所有字典的所有值。maps() 函式用於顯示 ChainMap 中所有字典的鍵及其對應值。
示例
# keys(), values() and maps # importing collections for ChainMap operations import collections # initializing dictionaries dic1 = { 'Program1' : 'Python', 'Program2' : 'HTML' } dic2 = { 'Program3' : ' Java', 'Program4' : 'C' } # initializing ChainMap chain = collections.ChainMap(dic1, dic2) # printing chainMap using maps print ("All the ChainMap contents are : ") print (chain.maps) # printing keys using keys() print ("All keys of ChainMap are : ") print (list(chain.keys())) # printing keys using keys() print ("All values of ChainMap are : ") print (list(chain.values()))
以下是上述程式碼的輸出:
All the ChainMap contents are : [{'Program1': 'Python', 'Program2': 'HTML'}, {'Program3': ' Java', 'Program4': 'C'}] All keys of ChainMap are : ['Program3', 'Program4', 'Program1', 'Program2'] All values of ChainMap are : [' Java', 'C', 'Python', 'HTML']
ChainMap 中的方法
以下是 ChainMap() 類中定義的方法:
方法 | 功能 |
---|---|
new_child() | 在 ChainMap 的開頭新增一個新的字典或迭代器 |
reversed() | 反轉 ChainMap 中字典或迭代器的相對順序 |
Python ChainMap.new_child() 方法
new_child() 方法用於在 ChainMap() 的開頭新增一個新的字典或迭代器。
示例
在以下示例中,我們使用 new_child() 向 ChainMap() 添加了一個新的字典:
from collections import ChainMap list1 = ['a','b','c','d'] list2 = [1, 2, 3, 4] list3 = ['one','two','three'] chain_list = ChainMap(list1, list2) print(chain_list) print(chain_list.new_child(list3))
以下是上述程式碼的輸出:
ChainMap(['a', 'b', 'c', 'd'], [1, 2, 3, 4]) ChainMap(['one', 'two', 'three'], ['a', 'b', 'c', 'd'], [1, 2, 3, 4])
Python ChainMap.reversed() 方法
reversed() 方法用於反轉 ChainMap 中的字典或迭代器。
示例
以下是 ChainMap 中 reversed() 方法的示例:
from collections import ChainMap # Define multiple lists list1 = ['a', 'b', 'c', 'd'] list2 = [1, 2, 3] list3 = ['one', 'two'] # Create a ChainMap with the first two lists chain_list = ChainMap(list1, list2) # Print the initial ChainMap print("Original ChainMap -", chain_list) # Reverse the order of maps in the original ChainMap (note: reversed() does not modify the list) reversed_maps = list(reversed(chain_list.maps)) # This creates a reversed list # Print the reversed order of maps (for demonstration) print("Reversed maps -", reversed_maps)
以下是上述程式碼的輸出:
Original ChainMap - ChainMap(['a', 'b', 'c', 'd'], [1, 2, 3]) Reversed maps - [[1, 2, 3], ['a', 'b', 'c', 'd']] Original maps after reversal attempt: [['a', 'b', 'c', 'd'], [1, 2, 3]]