[英]how to terminate the program when similar elements are found
I have the following 2D list:我有以下二维列表:
test_list = [['A', 'B', 'C'], ['I', 'L', 'A', 'C', 'K', 'B'], ['J', 'I', 'A', 'B', 'C']]
I want to compare the 1st list elements of the 2D array test_list[0]
with all other lists.我想将二维数组
test_list[0]
的第一个列表元素与所有其他列表进行比较。 If the elements ['A', 'B', 'C']
are present in all other lists then it should print any message such as "All elements are similar" and the program should terminate when it finds the above condition如果元素
['A', 'B', 'C']
出现在所有其他列表中,那么它应该打印任何消息,例如“所有元素都是相似的”,并且程序应该在找到上述条件时终止
I have tried this piece of code but it only needs a termination condition: this is only a best-case scenario in which all elements are present.我试过这段代码,但它只需要一个终止条件:这只是所有元素都存在的最佳情况。
test_list = [['A', 'B', 'C'], ['I', 'L', 'A', 'C', 'K', 'B'], ['J', 'I', 'A', 'B', 'C']]
s = test_list[0]
for e in test_list[1:]:
if all(v in e for v in s):
print(e, "contains all elements of ", s)
#the program should terminate only if all the members are present.
You can use another all()
call to test all of test_list[1:]
您可以使用另一个
all()
调用来测试所有test_list[1:]
s = test_list[0]
if all(all(v in e for v in s) for e in test_list[1:]):
print("All elements are similar")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.