python中大于x且小于y的元素的表达式,全部在一个return语句中.没有循环.蟒蛇

-6 python numpy

返回数组A中大于x且小于y的项数.A是一维浮点数组,x和y是浮点数.例如F6(np.array([1.1, 2.2, 3.3, 2.3]), 2, 3)应该返回2.

def F(A,x,y):
     return ________?
Run Code Online (Sandbox Code Playgroud)

只能使用此行.在python中

Ery*_*Sun 5

这是将其作为矢量化操作的一种方法:

import numpy as np

def F(A, x, y):
    return np.sum((A > x) & (A < y))
Run Code Online (Sandbox Code Playgroud)

每个比较返回一个布尔数组.表达式ANDs这两个数组元素并将结果相加.