Skip to main content
 首页 » 编程设计

python之在 Python 中编写一个函数,该函数返回数组中大于零的数字的计数

2025年05月04日10三少

例如,假设我们调用函数 countPositive([1,2,3]),它将返回 3。或者如果我们有数组 [-1,0,1],则有一个正数,所以该函数将返回 1。依此类推。

请您参考如下方法:

def countPositive(nums): 
        return sum(1 for x in nums if x > 0)