Python – 测试列表中的元素是否在另一个列表中的最小值/最大值范围内
当需要测试元素是否在最大值/最小值范围内时,需要遍历列表元素并检查该元素是否等于“max”值。
示例
以下是该示例的演示
my_list = [5, 6, 4, 7, 8, 13, 15]
print("The list is : ")
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")输出
The list is : [5, 6, 4, 7, 8, 13, 15] All the elements are not in the min/max range
说明
定义了一个列表并在控制台上显示。
定义了另一个整数列表。
将一个变量赋值为“True”。
遍历整数列表中的值。
如果列表中元素的最大值与整数列表中的任何元素不等,则结果变量将设置为“False”。
退出循环。
最后,检查该值是否为“True”。
根据此条件,在控制台上显示相关结果。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP