[Python]生成器表達式過濾多層的列表中的空值

[Python]生成器表達式過濾多層的列表中的空值

更新於 發佈於 閱讀時間約 4 分鐘

在處理數據時,最可能會遇到數據中含有None的時候,若沒有處理就進行運算就會造成程式崩潰或者報錯

數據中含有None

input_list = [(42, 292), (28, 296), (999, 92), (993, 46), (219, 4), (279, 2), (None, None), (None, None)]

本文將將介紹使用生成器表達式(generator expression)來過濾掉空值保留好的數據,也用其他方法來達到一樣的效果。


程式範例

生成器表達式(generator expression)

input_list = [(42, 292), (28, 296), (999, 92), (993, 46), (219, 4), (279, 2), (None, None), (None, None)]

# 使用生成器表達式來過濾掉包含 None 的座標
filtered_list = list((x, y) for x, y in input_list if x is not None and y is not None)

print(filtered_list) # 這會輸出 [(42, 292), (28, 296), (999, 92), (993, 46), (219, 4), (279, 2)]

生成器表達式

(x, y) for x, y in input_list if x is not None and y is not None

詳細步驟

for x, y in input_list
遍歷 input_list: 生成器表達式會依次取出 input_list 中的每一個二元組 (x, y)

input_list if x is not None and y is not None
過濾條件: 對於每一個二元組 (x, y),檢查 x 是否為 None 並且 y 是否為 None。如果 xy 都不是 None,則保留該二元組;否則,過濾掉該二元組。

轉換成列表list(...)。

以下用其他方法也可以做到過濾None的值

列表推導式(list comprehension)

input_list = [(42, 292), (28, 296), (999, 92), (993, 46), (219, 4), (279, 2), (None, None), (None, None)]

filtered_list = [(x, y) for x, y in input_list if x is not None and y is not None]

print(filtered_list) # 這會輸出 [(42, 292), (28, 296), (999, 92), (993, 46), (219, 4), (279, 2)]

使用 filter 函數過濾 None

input_list = [(42, 292), (28, 296), (999, 92), (993, 46), (219, 4), (279, 2), (None, None), (None, None)]

filtered_list = list(filter(lambda coord: coord[0] is not None and coord[1] is not None, input_list))

print(filtered_list) # 這會輸出 [(42, 292), (28, 296), (999, 92), (993, 46), (219, 4), (279, 2)]

使用 for 迴圈手動過濾 None

input_list = [(42, 292), (28, 296), (999, 92), (993, 46), (219, 4), (279, 2), (None, None), (None, None)]

filtered_list = []
for coord in input_list:
if coord[0] is not None and coord[1] is not None:
filtered_list.append(coord)

print(filtered_list) # 這會輸出 [(42, 292), (28, 296), (999, 92), (993, 46), (219, 4), (279, 2)]

這些方法都可以保留原本的非空值,並且過濾掉包含 None 的座標。你可以根據自己的需求選擇最適合的一種方法。


avatar-img
螃蟹_crab的沙龍
138會員
248內容數
本業是影像辨識軟體開發,閒暇時間進修AI相關內容,將學習到的內容寫成文章分享。
留言
avatar-img
留言分享你的想法!
螃蟹_crab的沙龍 的其他內容
1. 什麼是虛擬環境 (venv)? 虛擬環境就是讓你在同一台電腦上,可以為不同的專案建立「獨立」的 Python 環境,互不干擾。 例如:某個專案用 Dash 3.0.4,另一個用 Dash 4.0,不會互相影響。 2. 如何建立一個新的 venv? 打開終端機 Terminal / C
在 Python 中,字典(dict)是非常常用的資料結構,用來儲存 key-value 配對。而在操作字典時,.setdefault() 是一個常被忽略但非常實用的方法。 這篇文章會完整介紹 .setdefault() 的語法、用途、與實務範例,幫助你更有效率地處理字典資料。 什麼是 .se
在軟體開發或部署過程中,我們經常需要對多個 .ini 設定檔進行批次修改,例如將某個舊的參數名稱或路徑名稱,替換成新的命名。 這就像記事本的「全部取代」功能,但你不需要一個個打開檔案慢慢點——你可以用 Python 自動完成這些事! 本文將介紹一個 Python 函式:search_and_re
1. 什麼是虛擬環境 (venv)? 虛擬環境就是讓你在同一台電腦上,可以為不同的專案建立「獨立」的 Python 環境,互不干擾。 例如:某個專案用 Dash 3.0.4,另一個用 Dash 4.0,不會互相影響。 2. 如何建立一個新的 venv? 打開終端機 Terminal / C
在 Python 中,字典(dict)是非常常用的資料結構,用來儲存 key-value 配對。而在操作字典時,.setdefault() 是一個常被忽略但非常實用的方法。 這篇文章會完整介紹 .setdefault() 的語法、用途、與實務範例,幫助你更有效率地處理字典資料。 什麼是 .se
在軟體開發或部署過程中,我們經常需要對多個 .ini 設定檔進行批次修改,例如將某個舊的參數名稱或路徑名稱,替換成新的命名。 這就像記事本的「全部取代」功能,但你不需要一個個打開檔案慢慢點——你可以用 Python 自動完成這些事! 本文將介紹一個 Python 函式:search_and_re