用于移除具有自定义列表元素行的 Python 程序
在需要移除具有自定义列表元素的行时,会用到列表解析和“any”运算符。
示例
以下是相同示例的演示
my_list = [[14, 3, 11], [66, 27, 8], [31, 12, 21], [11, 16, 26]]
print("The list is :")
print(my_list)
check_list = [3, 10, 19, 29, 20, 15]
print("The check list is :")
print(check_list)
my_result = [row for row in my_list if not any(element in row for element in check_list)]
print("The result is :")
print(my_result)输出
The list is : [[14, 3, 11], [66, 27, 8], [31, 12, 21], [11, 16, 26]] The check list is : [3, 10, 19, 29, 20, 15] The result is : [[66, 27, 8], [31, 12, 21], [11, 16, 26]]
说明
- 定义了一个整数列表,并在控制台上显示。
- 将一个整数列表定义为“check_list”,并在控制台上显示。
- 使用列表解析遍历元素,并使用“any”运算符。
- 此处,检查行中的元素是否与“check_list”中提供的元素匹配。
- 如果匹配,该行就存储在列表中。
- 这被赋值给变量。
- 它在控制台上显示为输出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP