numpy np.where多重条件 [英] Numpy np.where multiple condition
本文介绍了numpy np.where多重条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用numpy处理多个条件.
I need to work with multiple condition using numpy.
我正在尝试似乎有效的这段代码.
I'm trying this code that seem to work.
我的问题是:还有另一种方法可以完成相同的工作吗?
My question is: There is another alternative that can do the same job?
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")))
这篇关于numpy np.where多重条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文