python - Numpy np.where 多重条件

标签 python numpy where-clause

我需要使用 numpy 处理多个条件。

我正在尝试这段似乎有效的代码。

我的问题是:是否有另一种替代方案可以完成同样的工作?

Mur=np.array([200,246,372])*pq.kN*pq.m
Mumax=np.array([1400,600,700])*pq.kN*pq.m
Mu=np.array([100,500,2000])*pq.kN*pq.m
Acreq=np.where(Mu<Mur,0,"zero")
Acreq=np.where(((Mur<Mu)&(Mu<Mumax)),45,Acreq)
Acreq=np.where(Mu>Mumax,60,Acreq)
Print(Acreq)
['0' '45' '60']

最佳答案

从这里开始:

Mur    = np.array([200,246,372])*3*5
Mumax  = np.array([1400,600,700])*3*5
Mu     = np.array([100,500,2000])*3*5
Acreq  = np.where(Mu<Mur,0,"zero")
Acreq  = np.where((Mur<Mu)&(Mu<Mumax),45,Acreq)
Acreq  = np.where(Mu>Mumax,60,Acreq)

print(Acreq)

['0' '45' '60']

试试这个:

conditions  = [Mu<Mur, (Mur<Mu)&(Mu<Mumax), Mu>Mumax ]
choices     = [ 0, 45, 60 ]
Acreq       = np.select(conditions, choices, default='zero')
print(Acreq)


['0' '45' '60']

这也有效:

np.where((Mur<Mu)&(Mu<Mumax),45,np.where(Mu>Mumax,60,np.where(Mu<Mur,0,"zero")))

关于python - Numpy np.where 多重条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39307268/

相关文章:

python - 有没有办法让我查看应用程序输出的音量?

python - Numpy.dot 错误?不一致的 NaN 行为

python - 如何计算 50x20 矩阵的类内散布

mysql - CodeIgniter MySQL 一次更新多个表 db->update() db->where()

java - 解析未知的 XML 派生文件

python - 无法在 Mac OS X 10.7 上的 virtualenv 中使用 pip 安装 psycopg2

Python multiprocessing.pool 与类目标函数和神经进化的交互

python - numpy 读取某些字段有逗号的 CSV 文件?

mysql group by 语句中的where条件?

mysql - 如何从值不是...的 mysql 表中进行选择?