我正在尝试生成一个程序来检查2D数组的最高和值。然后它需要将每个值相互比较到输出,这是数组中每个位置的所有总和中的最大值。
我能够为一定数量的元素执行此操作,但我想根据用户输入的元素数量为无限数量的元素执行此操作。
Person = [123,321,213]
TotalMorn =[[3,5,6,4,5,3,7],
[2,1,1,2,3,4,5],
[3,4,5,6,7,6,5]]
TotalAft =[[8,7,6,2,6,7,5],
[4,3,2,3,4,3,2],
[1,1,1,1,2,3,1]]
def WeeklyTotal(K):
Total = 0
for j in range(7):
TotalM = TotalMorn[K][j]
TotslA = TotalAft[K][j]
Total = TotalM + TotalA + Total
return Total
Total1 = WeeklyTotal(0)
Total2 = WeeklyTotal(1)
Total3 = WeeklyTotal(2)
if Total1 > Total2 and Total1 > Total3:
print 'Person', (Person[0]), 'Has produced the most profit with ', Total1
elif Total2 > Total1 and Total2 > Total3:
print 'Peron', (Person[1]), 'Has produced the most profit with ', Total2
elif Total3 > Total1 and Total3 > Total2:
print 'Person', (Person[2]), 'Has produced the most profit with ', Total3
答案 0 :(得分:0)
在您的情况下,您需要将 person 绑定到他的 totalScore 。然后,您将查找最高分数值,因此您将拥有与此值相关联的最高分数的人,例如......
arr = [('John', 15), ('Sue', 13), ('Jack', 20), ('Tom', 5)]
print max(arr, key = lambda x:x[1])
# outputs : ('Jack', 20)
在这里,您已将得分绑定到人到元组列表。您要求 max 函数使用每个元组的第二个元素查找最高值(这就是key = lambda x:x[1]
的意思)。
在您的示例中,数据的格式如...
people = ['John', 'Sue', 'Jack', 'Tom']
scores = [15, 13, 20, 5]
...你可能想用一个函数来确定最高值的索引,这样做:
# We set the highest score at the beginning as the first value, and save the index.
curr_score = scores[0]
saved_index = 0
# We then iterate through scores, not looping through the first item, as its index
# is already saved. We set the start of the index to `1`.
for index, score in enumerate(scores[1:], start=1):
if score > curr_score:
# We save the new highest score.
curr_score = score
# And keep its index.
saved_index = index
# At the end of the loop, we saved the maximum value index, and can use it
# on `people` array.
print people[saved_index]
# It will output the person with the highest score.
答案 1 :(得分:0)
我假设您要做的是在数组WeeklyTotal中找到最大值?
maxIndex, maxValue = 0,0
for i in range(0,len(WeeklyTotal)-1):
if WeeklyTotal[i] > maxValue :
maxValue = WeeklyTotal[i]
maxIndex = i
print 'Person ',Person[maxIndex],' Has produced the most profit with ',WeeklyTotal[maxIndex]
答案 2 :(得分:0)
这是一个使用一些python习语的简短解决方案:
(感谢IMCoins提醒我max(a, key = lambda...
)
people = [123,321,213]
morning =[[3,5,6,4,5,3,7],
[2,1,1,2,3,4,5],
[3,4,5,6,7,6,5]]
afternoon =[[8,7,6,2,6,7,5],
[4,3,2,3,4,3,2],
[1,1,1,1,2,3,1]]
def makeTotals(people, morning, afternoon):
return [(p,sum(m+a)) for (p,m,a) in zip(people, morning, afternoon)]
allTotals = makeTotals(people, morning, afternoon)
print(max(allTotals, key = lambda x:x[1]))
输出:
(123, 74)
makeTotals
函数zip
向上排列数组,以便将每个人的早晨和下午分数对齐,(p,sum(m+a))
创建tuple
此人及其总数。< / p>
对max
的调用使用key = lambda x:x[1]
参数(感谢@IMCoins)提取上午和下午得分最大的元组。
答案 3 :(得分:0)
一个班轮怎么样&#39; (带有换行符和添加的空格以便于阅读)
Person = [123,321,213]
TotalMorn =[[3,5,6,4,5,3,7],
[2,1,1,2,3,4,5],
[3,4,5,6,7,6,5]]
TotalAft =[[8,7,6,2,6,7,5],
[4,3,2,3,4,3,2],
[1,1,1,1,2,3,1]]
print('Person %s Has produced the most profit with %s'
% max(zip(map(sum,
zip(map(sum, TotalMorn),
map(sum, TotalAft))),
Person))[::-1])
Person 123 Has produced the most profit with 74