我对Python和MapReduce完全陌生。如果有人能帮助我取得以下成果,那就太好了。我想从下面的列表中计算出键的计数和每个键的平均值。对中的第一个数字是键,第二个是值。
输出如下所示。
谢谢
发布于 2022-08-06 13:55:35
我建议您使用迭代工具而不是减少。
import itertools
import functools
import statistics
data = [[1,5], [1,5], [2,7], [2,8], [1,10], [2,10], [3,3], [1,20]]
# First, sort and group the input by key
sorted_data = sorted(data, key=lambda x: x[0])
grouped = itertools.groupby(sorted_data, lambda e: e[0])
# This will result in a structure like this:
# [
# (1, [[1, 5], [1, 5], [1, 10], [1, 20]]),
# (2, [[2, 7], [2, 8], [2, 10]]),
# (3, [[3, 3]])
# ]
# Remove the duplicate keys from the structure
remove_duplicate_keys = map(lambda x: (x[0], [e[1] for e in x[1]]), grouped)
# This will produce the following structure:
# [
# (1, [5, 5, 10, 20]),
# (2, [7, 8, 10]),
# (3, [3])
# ]
# Now, calculate count and mean for each entry
result = map(lambda x: (x[0], len(x[1]), statistics.mean(x[1])), remove_dublicate_keys)
# This will result in the following list:
# [(1, 4, 10), (2, 3, 8.333333333333334), (3, 1, 3)]注意:所有指令都将返回生成器。这意味着python在开始使用之前不会计算任何内容。但您只能访问元素一次。如果需要将它们列在常规列表中,或者需要多次访问信息,请将最后一行替换为:
result = list(map(lambda x: (x[0], len(x[1]), statistics.mean(x[1])), remove_dublicate_keys))这将将原始生成器链转换为常规列表。
https://stackoverflow.com/questions/73259921
复制相似问题