我有一个Python3代码,它返回两个字典(两个都有相同的索引),第一个字典包含名称列表,第二个字典包含列表分数。在
对于每个索引,列表可能包含重复的名称,并且可能包含不同的名称,如下面的代码所示。在
我怎样才能得到字典的每个索引,列表中每个人的平均值
我尝试对索引执行嵌套for循环,以提取每个索引上的列表
然后,我在名称列表中的每个项目上循环,保存该名称的索引,然后重新循环以获得平均值names = {"0":['name1', 'name1', 'name2', 'name1'] , "1":['name1', 'name2', 'name2', 'name2', 'name2']}
scores = {"0":[0.5, 0.5, 1, 0.2], "1":[0.2, 0.8, 0.8, 0.9, 0.9]}
for ind in names:
namelist = names[ind]
scoreist = scores[ind] #as ind in names is the same as in scores
.
.
.
indices = list()
counter = 0
tempname = namelist[0]
for name in names:
if name == tempname:
indices.append(counter)
counter += 1
total = 0
for index in indicies:
total += scorlist[index]
average = total / scorelist
.
.
.
然后我弹出列表的索引并重新迭代。
我知道应该有一个更快、更干净的方法来完成这项工作,而不是在while循环中嵌套循环。。。在
编辑:
输出应该是
^{pr2}$