Python - 提取包含任意布尔真值的列
当需要提取包含任意布尔真值的列时,可以使用列表推导和“any”运算符。
以下是对此内容的说明——
示例
my_tuple = [[False, True], [False, False], [True, False, True], [False]]
print("The tuple is :")
print(my_tuple)
my_result = [row for row in my_tuple if any(element for element in row)]
print("The result is ")
print(my_result)输出
The tuple is : [[False, True], [False, False], [True, False, True], [False]] The result is [[False, True], [True, False, True]]
说明
定义了一个列表的列表,并显示在控制台中。
使用列表推导来检查列表中是否存在任何元素。
“any”运算符给出真或假的结果。
将其转换为列表并赋值给变量。
这是显示在控制台上的输出内容。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP