如果列表中不包含任何字元,請從 Python 元組列表中移除元組
當需要根據給定條件從元組列表中移除一個元組時,即元組不包含特定字元,可以使用列表解析。
可以使用列表來儲存異構值(即任何資料型別的資料,如整數、浮點數、字串等)。元組列表基本上包含一些包含在列表中的元組。
列表解析是遍歷列表並執行其上操作的一種簡寫。
以下是同一演示:
示例
my_list = [('. ', 62), ('Mark', 5),
('Paul.', 21), ('.....', 0),
('-Jane', 115), ('Jake', 15),
('::', 63), ('John', 3), ('--', 1),
('there', 82), ('Harold', 100)]
my_result = [(a, b) for a, b in my_list
if any(c.isalpha() for c in a)]
print("The tuple that doesn't contain any character has been removed")
print("The resultant list of tuple is :")
print(my_result)輸出
The tuple that doesn't contain any character has been removed
The resultant list of tuple is :
[('Mark', 5), ('Paul.', 21), ('-Jane', 115), ('Jake', 15), ('John', 3), ('there', 82), ('Harold', 100)]說明
- 定義了一個元組列表,並在控制檯上顯示。
- 該元組列表被遍歷,並檢查它是否屬於字母族。
- 此操作的資料儲存在一個變數中。
- 此變數是顯示在控制檯上的輸出。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP