如何从输入中获取多个值并显示标准输出中的最大值?



>我有一个标准输出,如下所示:

seventy-five 0.050
states 0.719
drainage-basin 0.037
scotland 0.037
reading 0.123
thirty-eight 0.000
almost 0.037
rhine 0.000
proper 0.037
contrary 0.087

如何定义一个方法来输入几个字符串,如"状态",它将返回其值"0.719"及其结论;

Enter query words, one per line. Blank line to end: 
proper
almost
rhine

然后他们返回:

states 0.719
almost 0.037
rhine 0.000
largest value is states

否则返回"项目不在列表中"。由于我是初学者,我真的不知道如何处理标准输出。我试过:

result = input("Enter query words, one per line. Blank line to end: ")
if result in STD:
the_result = STD[result]
else:
print("item not in list")

您可以使用字典STD来存储数据。读取用户输入并将其对应的数据存储为每个输入,作为字典the_result中的键和值。

最后,字典the_result中最大值对应的键是最大的项。

import operator
STD = {'seventy-five': 0.050,
'states': 0.719,
'drainage-basin': 0.037,
'scotland': 0.037,
'reading': 0.123,
'thirty-eight': 0.000,
'almost': 0.037,
'rhine': 0.000,
'proper': 0.037,
'contrary': 0.087}
print("Enter query words, one per line. Blank line to end: ")
result = []
while True:
line = input()
if line:
result.append(line)
else:
break
the_result = {}
for each in result:
if each in STD:
the_result.update({each: STD[each]})
print("{} {}".format(each, STD[each]))
else:
print("{} not in list".format(each))
print("largest value is {}".format(max(the_result.items(), key=operator.itemgetter(1))[0]))

最新更新