Python – 带有全部列表元素的行
如果需要提供带有所有列表元素的行,则使用标志值、简单迭代和“追加”方法。
示例
以下是相同的演示
my_list = [[8, 6, 3, 2], [1, 6], [2, 1,7], [8, 1, 2]]
print("The list is :")
print(my_list)
sub_list = [1, 2]
result = []
for row in my_list:
flag = True
for element in sub_list:
if element not in row:
flag = False
if flag:
result.append(row)
print("The resultant list is :")
print(result)输出
The list is : [[8, 6, 3, 2], [1, 6], [2, 1, 7], [8, 1, 2]] The resultant list is : [[2, 1, 7], [8, 1, 2]]
说明
定义一个列表列表并显示在控制台上。
定义另一个带有整数值的列表。
定义另一个空列表。
对列表列表进行迭代,并将标志值设置为“True”。
如果整数列表中存在元素不在列表中,则将标志值设置为“False”。
最后,根据标志值确定输出。
如果标志的值为“True”,则将元素追加到空列表中。
这在控制台上显示为输出。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP