这是我的字典列表:
array_of_dictionaries = [{
"name": "Budi",
"age": 23,
"test_scores": [100.0, 98.0, 89.0]
},
{
"name": "Charlie",
"age": 24,
"test_scores": [90.0, 100.0]
}]
这是我的代码:
def get_all_time_max_avg_score(dictionary_list):
for element in dictionary_list:
lensum = len(element['test_scores'])
elesum = sum(element['test_scores'])
meansum = elesum / lensum
for attribute in element.keys():
print("Mr. " + str(element['name']) + " with age(years) " + str(element['age']) + " get the highest average scores, " + str(round(meansum, 2)))
break
get_all_time_max_avg_score(array_of_dictionaries)
所以我想从这两本字典中得到最高的平均分。我想要的输出是:
Mr. Budi with age(years) 23 get the highest average scores, 95.67
但我得到的是:
Mr. Budi with age(years) 23 get the highest average scores, 95.67
Mr. Charlie with age(years) 24 get the highest average scores, 95.0
我真的很感激能得到的每一个帮助
这段代码迭代并打印列表中的每个元素。 相反,通过与平均值进行比较,对列表执行
max
下面是一个具有列表理解功能的解决方案。
产出:
在打印任何内容之前,您需要计算每一项的平均值
或者使用
Dataframe
使用带有自定义键的内置
max
函数查找平均分数最高的条目相关问题 更多 >
编程相关推荐