python中的函数什么都不返回 [英] Where function in python returns nothing
本文介绍了python中的函数什么都不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数组
a = array([1,5,7])
我应用where函数
where(a==8)
在这种情况下返回的是
(array([], dtype=int64),)
但是,只要where函数返回一个空数组,我都希望代码返回整数"0".有可能吗?
However I would like the code to return the integer "0" whenever the where function returns an empty array. Is that possible?
推荐答案
def where0(vec):
a = where(vec)
return a if a[0] else 0
# The return above is equivalent to:
# if len(a[0]) == 0:
# return 0 # or whatever you like
# else:
# return a
a = array([1,5,7])
print where0(a==8)
还要考虑您的问题下 aix 的评论.不用修正where()
,而是修正您的算法
And consider also the comment from aix under your question. Instead of fixing where()
, fix your algorithm
这篇关于python中的函数什么都不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文