admin管理员组文章数量:1130349
Python空数组判断
在使用numpy找到指定元素对应索引时,一般会使用numpy.argwhere()或numpy.where()函数。
正常使用numpy.argwhere()函数时,返回结果是元素的坐标。
但是当数组内没有指定元素时,会返回[],比如下面这段程序
import numpy
x = numpy.arange(5) #[0,1,2,3,4]
y = numpy.argwhere(x==5)
输出y时结果为[]
print(y) #[]
查看y的布尔值时,结果为False
print(bool(y)) #False
并报错
DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
根据Python提示,可以使用array.size来判断数组是否为空。
print(y.size) # 0
修改程序发现空数组的值为0,而非空数组的值>0。
因此可以使用这个方式来判断空数组。
Python空数组判断
在使用numpy找到指定元素对应索引时,一般会使用numpy.argwhere()或numpy.where()函数。
正常使用numpy.argwhere()函数时,返回结果是元素的坐标。
但是当数组内没有指定元素时,会返回[],比如下面这段程序
import numpy
x = numpy.arange(5) #[0,1,2,3,4]
y = numpy.argwhere(x==5)
输出y时结果为[]
print(y) #[]
查看y的布尔值时,结果为False
print(bool(y)) #False
并报错
DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
根据Python提示,可以使用array.size来判断数组是否为空。
print(y.size) # 0
修改程序发现空数组的值为0,而非空数组的值>0。
因此可以使用这个方式来判断空数组。
本文标签: Python空数组判断
版权声明:本文标题:Python空数组判断 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/IT/1694638088a254365.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论