
- 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 - 套接字程式設計
- 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 - GUIs
- 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 - 迴圈集合
遍歷集合項
在 Python 中遍歷集合項是指迭代集合中的每個元素。之後我們可以對每個專案執行所需的運算。這些操作包括列出列印元素、條件操作、過濾元素等。
與列表和元組不同,集合是無序的集合,因此元素將以任意順序訪問。您可以使用for迴圈來迭代集合中的項。
使用 For 迴圈遍歷集合項
Python 中的 for 迴圈用於迭代序列(如列表、元組、字典、字串或範圍)或任何其他可迭代物件。它允許您為序列中的每個專案重複執行一段程式碼。
在 for 迴圈中,您可以使用變數訪問序列中的每個專案,從而允許您根據該專案的價值執行操作或邏輯。我們可以使用 for 迴圈遍歷集合項,方法是迭代集合中的每個項。
語法
以下是使用 Python 中的 for 迴圈遍歷集合中的項的基本語法:
for item in set: # Code block to execute
示例
在以下示例中,我們使用 for 迴圈迭代集合“my_set”中的每個元素並檢索每個元素:
# Defining a set with multiple elements my_set = {25, 12, 10, -21, 10, 100} # Loop through each item in the set for item in my_set: # Performing operations on each element print("Item:", item)
輸出
以下是上述程式碼的輸出:
Item: 100 Item: 25 Item: 10 Item: -21 Item: 12
使用 While 迴圈遍歷集合項
Python 中的 while 迴圈用於重複執行一段程式碼,只要指定的條件計算結果為“True”。
我們可以使用 while 迴圈遍歷集合項,方法是將集合轉換為迭代器,然後迭代每個元素,直到迭代器到達集合的末尾。
迭代器是一個物件,允許你遍歷集合(例如列表、元組、集合或字典)中的所有元素,每次一個元素。
示例
在下面的示例中,我們使用迭代器和 while 迴圈遍歷一個集合。 "try" 程式碼塊檢索並列印每個專案,而 "except StopIteration" 程式碼塊在沒有更多專案可獲取時中斷迴圈 -
# Defining a set with multiple elements my_set = {1, 2, 3, 4, 5} # Converting the set to an iterator set_iterator = iter(my_set) # Looping through each item in the set using a while loop while True: try: # Getting the next item from the iterator item = next(set_iterator) # Performing operations on each element print("Item:", item) except StopIteration: # If StopIteration is raised, break from the loop break
輸出
以上程式碼的輸出如下所示 -
Item: 1 Item: 2 Item: 3 Item: 4 Item: 5
使用集合推導式迭代
Python 中的集合推導式是一種簡潔的方式,透過迭代可迭代物件並可選地應用條件來建立集合。它用於使用類似於列表推導式的語法生成集合,但結果為一個集合,確保所有元素都是唯一的且無序的。
我們可以透過在花括號 {} 中定義一個集合推導式表示式,並在表示式中指定迭代和條件邏輯來使用集合推導式進行迭代。以下是語法 -
result_set = {expression for item in iterable if condition}
其中,
表示式 - 它是在可迭代物件中每個專案上求值的表示式。
專案 - 它是一個表示可迭代物件中每個元素的變數。
可迭代物件 - 它是要迭代的集合(例如,列表、元組、集合)。
條件 - 它是一個可選的條件,用於過濾包含在結果集合中的元素。
示例
在這個例子中,我們使用集合推導式來生成一個集合,該集合包含原始列表 "numbers" 中偶數的平方 -
# Original list numbers = [1, 2, 3, 4, 5] # Set comprehension to create a set of squares of even numbers squares_of_evens = {x**2 for x in numbers if x % 2 == 0} # Print the resulting set print(squares_of_evens)
輸出
我們得到如下所示的輸出 -
{16, 4}
使用 enumerate() 函式遍歷集合
Python 中的 enumerate() 函式用於迭代可迭代物件,同時提供每個元素的索引。
我們可以透過將集合轉換為列表,然後應用 enumerate() 來迭代元素及其索引位置,從而使用 enumerate() 函式遍歷集合。以下是語法 -
for index, item in enumerate(list(my_set)): # Your code here
示例
在下面的示例中,我們首先將一個集合轉換為列表。然後,我們使用帶有 enumerate() 函式的 for 迴圈遍歷列表,檢索每個專案及其索引 -
# Converting the set into a list my_set = {1, 2, 3, 4, 5} set_list = list(my_set) # Iterating through the list for index, item in enumerate(set_list): print("Index:", index, "Item:", item)
輸出
生成的輸出如下所示 -
Index: 0 Item: 1 Index: 1 Item: 2 Index: 2 Item: 3 Index: 3 Item: 4 Index: 4 Item: 5
使用 add() 方法迴圈遍歷集合專案
Python 中的 add() 方法用於向集合中新增單個元素。如果該元素已存在於集合中,則集合保持不變。
我們不能直接使用 add() 方法迴圈遍歷集合專案,因為 add() 特別用於向集合中新增單個元素,而不是迭代現有元素。
要迴圈遍歷集合專案,我們使用諸如 for 迴圈或集合推導式之類的方法。
示例
在這個例子中,我們迴圈遍歷一系列數字,並使用 add() 方法將每個數字新增到集合中。迴圈迭代現有元素,而 add() 方法將新元素新增到集合中 -
# Creating an empty set my_set = set() # Looping through a sequence and adding elements to the set for i in range(5): my_set.add(i) print(my_set)
它將產生以下輸出 -
{0, 1, 2, 3, 4}