gpt4 book ai didi

python - 使用 Python 3 对数组进行排序

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:26 24 4
gpt4 key购买 nike

我无法对该数组进行排序以获得最高的总点数以及旁边的名称。

用于输入事件:

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()

输入房屋名称:

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 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)

以当程序的输出如下所示时为例:

{'House 3': [2, 3, 4], 'House 2': [7, 7, 5], 'House1': [4, 4, 2]}

我在获得总点数最高的房子并打印总冠军时遇到了困难。

感谢任何可以提供帮助的人。

最佳答案

max(scores, key=lambda h: sum(scores[h]))
# => 'House 2'

关于python - 使用 Python 3 对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32240958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com