python 中的函数什么都不返回

Bri*_*ian 2 python numpy where-clause

我有这个数组

     a = array([1,5,7])
Run Code Online (Sandbox Code Playgroud)

我应用了 where 函数

     where(a==8)
Run Code Online (Sandbox Code Playgroud)

在这种情况下返回的是

    (array([], dtype=int64),)
Run Code Online (Sandbox Code Playgroud)

但是,我希望代码在 where 函数返回空数组时返回整数“0”。那可能吗?

Jak*_* M. 5

def where0(vec):\n    a = where(vec)\n    return a if a[0]\xc2\xa0else 0\n    # The return above is equivalent to:\n    # if len(a[0]) == 0:\n    #     return 0  # or whatever you like\n    # else:\n    #     return a\n\na = array([1,5,7])\nprint where0(a==8)\n
Run Code Online (Sandbox Code Playgroud)\n\n

并考虑一下aix在你的问题下的评论。而不是修复where(),而是修复你的算法

\n