Determination condition in all () / any ()

Determination condition in all () / any ()

all() And  any() two functions suited for use in determination conditions. These two functions accept an iteration object that returns a Boolean value, where:

  • all(seq): Only  seq return when all objects are boolean true  True, otherwise returns False
  • any(seq): As long as  seq any object to a Boolean true is returned  True, otherwise False

If we have the following code:

DEF all_numbers_gt_10 ( Numbers):
     "" "only if all the sequence numbers greater than 10, return True " "" IF Not Numbers: return False for n- in Numbers: IF n- <= 10: return False return True 

If a  all() built-in function, together with a simple generator expression, the above code can be written as:

def all_numbers_gt_10_2(numbers):
    return bool(numbers) and all(n > 10 for n in numbers)



def all_numbers_gt_10_2(numbers):
    return bool(numbers) and all(n > 10 for n in numbers)
x = [-10]

print(all_numbers_gt_10_2(x))

False

 



Guess you like

Origin www.cnblogs.com/botoo/p/11994489.html