Python - 测试列表的元素是否在其他列表的最小/最大范围内
当需要测试元素是否在最小/最大范围内时,将迭代列表元素,并检查它是否等于“最大值”值。
示例
下面是相同的演示
my_list = [5, 6, 4, 7, 8, 13, 15] print("名单是: ") print(my_list) range_list = [4, 7, 10, 6] my_result = True for elem in range_list: if elem!= max(my_list): my_result = False break if(elem == True): print("All the elements are in the min/max range") else: print("All the elements are not in the min/max range")输出结果
名单是: [5, 6, 4, 7, 8, 13, 15] All the elements are not in the min/max range
解释
定义了一个列表并显示在控制台上。
定义了另一个整数列表。
变量被分配给“真”。
整数列表中的值被迭代。
如果原始列表中元素的最大值不等于整数列表中的任何元素,则结果变量设置为“假”。
它跳出循环。
最后,检查该值是否为“真”。
根据这一点,相关结果会显示在控制台上。