了解numpy数组中any()和all()的用法 [英] Understanding the use of any() and all() in numpy arrays

查看:61
本文介绍了了解numpy数组中any()和all()的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容有什么区别?

a = np.array([2,3,4])
b = np.array([2,7,8])

if a.any() == b.all():
   print('yes')

a = np.array([2,3,4])
b = np.array([2,7,8])

if a.any() == b.any():
   print('yes')

在两种情况下,均会打印'是'.

In both situations, 'yes' is printed.

推荐答案

any() all()用于布尔数组.如果数组中存在等于 True 的任何值,则 any()返回 True .如果数组中的所有值均等于 True ,则 all()返回 True .对于整数/浮点数,功能相似,不同之处在于,如果在数组中未找到值 0 ,它们将返回 True .在您的示例中,由于 a.any() a.all()都将返回 True ,因此紧随 a.any()== a.all().

any() and all() are intended for boolean arrays. any() returns True if there's any values that are equal to True in the array. all() returns True if all values in the array are equal to True. For integers/floats the functionality is similar, except that they return True if the value 0 is not found in the array. In your example, since both a.any() and a.all() will return True, it follows that a.any() == a.all().

尝试执行以下代码,以了解其工作原理.

Try executing the following code to see how it works in practice.

a = np.asarray([1,2,3])
b = np.asarray([-1,0,1])
c = np.asarray([True, False])

print(a.any())
print(a.all())

print(b.any())
print(b.all())

print(c.any())
print(c.all())

这篇关于了解numpy数组中any()和all()的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆