对Python中两个配对列表中的重复值求平均值 [英] Average the duplicated values from two paired lists in Python
问题描述
在我的代码中,我从不同的来源获得两个不同的列表,但是我知道它们的顺序相同.第一个列表(名称")包含一个键字符串列表,而第二个列表("result_values")是一系列浮点数.我需要使该对唯一,但是我不能使用字典,因为只会保留最后插入的值:相反,我需要对具有重复键的值进行平均(算术平均值).
in my code I obtain two different lists from different sources, but I know they are in the same order. The first list ("names") contains a list of keys strings, while the second ("result_values") is a series of floats. I need to make the pair unique, but I can't use a dictionary as only the last value inserted would be kept: instead, I need to make an average (arithmetic mean) of the values that have a duplicate key.
所需结果示例:
names = ["pears", "apples", "pears", "bananas", "pears"]
result_values = [2, 1, 4, 8, 6] # ints here but it's the same conceptually
combined_result = average_duplicates(names, result_values)
print combined_result
{"pears": 4, "apples": 1, "bananas": 8}
我唯一的想法涉及多次迭代,到目前为止,它是丑陋的...是否有解决此问题的好方法?
My only ideas involve multiple iterations and so far have been ugly... is there an elegant solution to this problem?
推荐答案
我还是会使用字典
averages = {}
counts = {}
for name, value in zip(names, result_values):
if name in averages:
averages[name] += value
counts[name] += 1
else:
averages[name] = value
counts[name] = 1
for name in averages:
averages[name] = averages[name]/float(counts[name])
如果您担心大型列表,那么我将使用itertools中的izip
替换zip
.
If you're concerned with large lists, then I would replace zip
with izip
from itertools.
这篇关于对Python中两个配对列表中的重复值求平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!