展开全部
all是如果iterable的所有元素62616964757a686964616fe59b9ee7ad9431333361316636不为0、''、False或者iterable为空,all(iterable)返回True,否则返回False;函数等价于:def all(iterable):
for element in iterable:
if not element:
return False
return True
any是如果iterable的任何元素不为0、''、False,all(iterable)返回True。如果iterable为空,返回False。函数等价于:def any(iterable):
for element in iterable:
if element:
return False
return True
假设有这样的一个功能,一个班级所有学生的成绩只用及格和不及格来表示,所有学生的成绩都及格了才能评上【合格班级】,只有评上【合格班级】的班级才有进入选优秀班级的资格,现在用一个列表分别表示一个班级,列表里的数值分别表示每个学生的成绩,60以上(含)表示及格,60分以下表示不及格,那么现在只要使用all([score>=60 for score in scores])就可以判断该班级是否有能评上合格班级了,不用上面那种等价于的写法。代码精炼简洁了。
现在评选优秀班级的标准是每个学生的整体成绩都在80分以上(含)才能评选上,现在用any即可解决问题,any([score>=80 for score in scores])这样就可以了
伪代码如下#该班级是否有资格进入评选
if all([score>=60 for score in scores]):
print '是合格班级'
if any([score>=80 for score in scores]):
print '同时也是优秀班级'
else:
print '淘汰'