Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

python - Numpy np.where multiple condition

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']
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Starting with this:

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']

Try this:

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']

This also works:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...