all()函数与any()函数

有时候对python中的数组进行操作时,会用到python中的all()any()函数,这里记录一下:
all():当可迭代对象为空时返回True。或者当可迭代对象中是否所有值都为True,所有值都为True,则返回True。否则返回False
any():当可迭代对象为空时返回False。或者当可迭代对象中是否存在一个为True的值,若存在,返回True,否则返回False
代码如下:

import numpy as np


class Debug:
    @staticmethod
    def pythonFunction():
        x = np.array([0, 1, 2])
        print(x.all())
        print(x.any())


if __name__ == '__main__':
    debug = Debug()
    debug.pythonFunction()
"""
False
True
"""

我们可以看到,数组中存在一个0值,等同于False,因此使用.all()时返回False,使用.any()时返回True。数组为空的情况这里略过,可以自行尝试。

码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

猜你喜欢

转载自blog.csdn.net/u011699626/article/details/112256497