Python | 从列表中移除空元组
如果需要从元组列表中移除空元组,可以使用一个简单的循环。
列表可以用来存储异构值(即任何数据类型的数据,如整数、浮点数、字符串等)。
元组列表基本上包含一个列表中封装的元组。
以下是一个演示,涉及相同的内容 -
示例
def remove_empty(my_tuple):
my_tuple = [t for t in my_tuple if t]
return my_tuple
my_tuple = [(), (), (''), (" " , " "), (45, 67, 35, 66, 74, 89, 100) , 'jane']
print("The tuple is : ")
print(my_tuple)
print("The method to remove empty tuples is being called...")
my_result = remove_empty(my_tuple)
print("The list of tuple after remvoing empty tuples is : ")
print(my_result)输出
The tuple is :
[(), (), '', (' ', ' '), (45, 67, 35, 66, 74, 89, 100), 'jane']
The method to remove empty tuples is being called...
The list of tuple after remvoing empty tuples is :
[(' ', ' '), (45, 67, 35, 66, 74, 89, 100), 'jane']说明
- 定义了一个名为“remove_empty”的方法,该方法将元组列表作为参数。
- 它遍历元组,仅在它们非空时返回值。
- 定义了一个元组列表,并在控制台中显示该列表。
- 通过传递此元组列表调用该方法。
- 此操作的数据分配给一个变量。
- 然后它在控制台中显示为输出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP