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
解释
定义了一个列表并显示在控制台上。
定义了另一个整数列表。
变量被分配给“真”。
整数列表中的值被迭代。
如果原始列表中元素的最大值不等于整数列表中的任何元素,则结果变量设置为“假”。
它跳出循环。
最后,检查该值是否为“真”。
根据这一点,相关结果会显示在控制台上。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短