代码:
a = np.array([[33,77,43],[32,33,55],[56,68,43],[45,45,67],[33,23,5]])
b=a.T
p=np.array([12,16,10,14,15])
def eachsales(position,price):
print('each salesman total amount',np.sum((position*price),axis=1))
i=np.where(np.sum((position*price),axis=1)==np.max(np.sum((position*price),axis=1)))
print('the amount of salesman',i,'is the highest')
eachsales(b,p)
我找到了最大值并且知道它在哪里。
它告诉我它在 array([1]
,但我希望它可以输出 1 (数字)。
如果是“1”,则打印“sales A”。如果是“2”,则打印“sales B”,依此类推。
each salesman total amount [2593 3107 2839]
the amount of salesman (array([1], dtype=int64),) is the highest
最佳答案
无需为此使用np.where()
。而是使用函数np.argmax()
,它直接返回索引。尝试下面的代码:
import numpy as np
import string
a = np.array([[33,77,43],[32,33,55],[56,68,43],[45,45,67],[33,23,5]])
b = a.T
p = np.array([12,16,10,14,15])
def eachsales(position,price):
print('each salesman total amount',np.sum((position*price),axis=1))
i = np.argmax(np.sum((position*price),axis=1))
print('the amount of salesman','Sales %s'%string.ascii_uppercase[i],'is the highest')
eachsales(b,p)
关于python - 最大在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51335766/