np.where允许您选择要为布尔类型查询分配的值,例如:
test = [0,1,2]
np.where(test==0,'True','False')
print test
['True','False','False']
这基本上是一个“if”语句。是否有一种Pythonic的方法,可以为numpy数组编写类似于“if、else if、else”的语句(具有不同的情况)?
以下是我的解决方案:
color = [0,1,2]
color = np.where(color==0,'red',color)
color = np.where(color==1,'blue',color)
color = np.where(color==2,'green',color)
print color
['red','blue','green']
但我想知道是否有更好的方法来做这件事。