数组排序与Python 3 [英] Sorting An Array with Python 3
本文介绍了数组排序与Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有排序这阵取分的最高总金额,并与旁边的名字麻烦。
有关输入事件的:
标志= TRUE
而标志:
尝试:
E = STR(输入(输入事件名称[XXX类型阻止]:))
如果E =='XXX':
标志=假
其他:
event.append(五)
除了ValueError错误:
打印(请输入一个字,谢谢。)
打印()
有关进入房子的名称:
标志= TRUE
而标志:
尝试:
H = STR(输入(Eneter府名[类型XXX停止]:))
如果h =='XXX':
标志=假
其他:
总=总+ 1
house.append(H)
除了ValueError错误:
打印(请输入一个字,谢谢。)
打印()
有关然后让事件和房屋的要点如下:
对我(的事件):
在(居)记者:
标志= TRUE
而标志:
尝试:
7吨= INT(输入端(输入%s的房子%的分数:%(J,I)))
如果7吨< 0:
打印(请输入上面的数字0感谢)
其他:
标志=假
除了ValueError错误:
打印(请输入一个数字上面,谢谢。)
在j不是分数:
分数[J] = []
分数[J] .append(7吨)
score.append(7吨)
s_house =(J),(7吨)
s_house_event.append(s_house)
标志= TRUE
s_h_and_e =(ⅰ),(s_house_event)
list1.append(s_h_and_e)
至于当程序的输出如下的示例:
{屋3:[2,3,4],屋2:[7,7,5],'House1':[4,4,2] }
我有麻烦众议院与得分最高总额和打印的总冠军。
感谢的人谁可以提供帮助。
解决方案
MAX(分数,关键=拉姆达H:SUM(得分[H]))
#=> 楼2
I am having trouble with sorting this array to get the highest total amount of points and with the name next to it.
For entering the events its:
Flag = True
while Flag:
try:
e = str(input("Enter Event names [Type XXX to stop]: "))
if e == 'XXX':
Flag = False
else:
event.append(e)
except ValueError:
print("Please enter a word, Thanks")
print()
For entering house names its:
Flag = True
while Flag:
try:
h = str(input("Eneter House names [Type XXX to stop]: "))
if h == 'XXX':
Flag = False
else:
total = total + 1
house.append(h)
except ValueError:
print("Please enter a word, Thanks")
print()
For then getting the points for events and house as follows:
for i in (event):
for j in (house):
Flag = True
while Flag:
try:
sevent = int(input("Enter %s's house for %s score: "%(j, i)))
if sevent < 0:
print("Enter a number above 0, Thanks")
else:
Flag = False
except ValueError:
print("Please enter a number above, Thanks")
if j not in scores:
scores[j] = []
scores[j].append(sevent)
score.append(sevent)
s_house = (j), (sevent)
s_house_event.append(s_house)
Flag = True
s_h_and_e = (i), (s_house_event)
list1.append(s_h_and_e)
As an example for when the output of the program as follows:
{'House 3': [2, 3, 4], 'House 2': [7, 7, 5], 'House1': [4, 4, 2]}
I am having trouble getting the House with the highest total sum of the points and printing an overall winner.
Thanks for anyone who can help.
解决方案
max(scores, key=lambda h: sum(scores[h]))
# => 'House 2'
这篇关于数组排序与Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文