# Python检查列表中的所有值是否都大于给定值

2021年4月30日15:29:34 发表评论 1,148 次浏览

``````Input : list = [10, 20, 30, 40, 50]
given value = 20
Output : No

Input : list = [10, 20, 30, 40, 50]
given value = 5
Output : Yes``````

``````# python program to check if all
# values in the list are greater
# than val using traversal

def check(list1, val):

# traverse in the list
for x in list1:

# compare with all the values
# with val
if val> = x:
return False
return True

# driver code
list1 = [ 10 , 20 , 30 , 40 , 50 , 60 ]
val = 5
if (check(list1, val)):
print "Yes"
else :
print "No"

val = 20
if (check(list1, val)):
print "Yes"
else :
print "No"``````

``````Yes
No``````

``````# python program to check if all
# values in the list are greater
# than val using all() function

def check(list1, val):
return ( all (x> val for x in list1))

# driver code
list1 = [ 10 , 20 , 30 , 40 , 50 , 60 ]
val = 5
if (check(list1, val)):
print "Yes"
else :
print "No"

val = 20
if (check(list1, val)):
print "Yes"
else :
print "No"``````

``````Yes
No``````