代码:
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”,依此类推。
每个推销员的总金额[2593 3107 2839]
推销员的数量(array([1],dtype = int64),)最高
答案 0 :(得分:2)
无需使用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)
答案 1 :(得分:1)
尝试一下:
import warnings
warnings.filterwarnings("ignore")
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.where(np.sum((position*price),axis=1)==np.max(np.sum((position*price),axis=1)))
print('the amount of salesman','Sales %s'%string.ascii_uppercase[i[0]-1],'is the highest')
eachsales(b,p)
输出:
each salesman total amount [2593 3107 2839]
the amount of salesman Sales A is the highest