我有一个模块,它应该在给定向量列表的情况下找到平均值。问题是返回值不一致。有时它给了我预期的输出,有时它没有。
def find_average_record(sen_set, voting_dict):
"""
Input: a set of last names, a voting dictionary
Output: a vector containing the average components of the voting records
of the senators in the input set
"""
count = len(sen_set)
total_set = voting_dict[sen_set.pop()]
for senator in sen_set:
for vote in voting_dict[senator]:
total_set[vote] += vote
return [x/count for x in total_set]
例子:
voting_dict = {'Klein': [-1,0,1], 'Fox-Epstein': [-1,-1,-1], 'Ravella': [0,0,1]}
find_average_record({'Fox-Epstein','Ravella'}, voting_dict)
# should =>[-0.5, -0.5, 0.0]