python 中的函数什么都不返回
我有这个数组,
a = array([1,5,7])
我应用了 where 函数
where(a==8)
在这种情况下返回的是
(array([], dtype=int64),)
但是我希望代码在 where 函数返回空数组时返回整数“0”。这可能吗?
I have this array
a = array([1,5,7])
I apply the where function
where(a==8)
What is returned in this case is
(array([], dtype=int64),)
However I would like the code to return the integer "0" whenever the where function returns an empty array. Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还要考虑您的问题下 aix 的评论。不要修复
where()
,而是修复你的算法And consider also the comment from aix under your question. Instead of fixing
where()
, fix your algorithm最好使用只有一种返回类型的函数。您可以检查数组的大小以了解它是否为空,这应该可以完成工作:
Better use a function that has only one return type. You can check for the size of the array to know if it's empty or not, that should do the work:
空数组将返回 0 和 .size
a empty array will return 0 with .size
尝试下面的方法。这将处理返回索引 0 时等于 0 的测试失败的情况。 (例如以下情况下的
np.where(a==1
))Try the below. This will handle the case where a test for equality to 0 will fail when index 0 is returned. (e.g.
np.where(a==1
) in the below case)