我有一个元组列表,例如:(A, 1), (B, 2), (C, 3), (A, 9), (B, 8)
.
如何获取引用元组第一个元素的每个值的平均值,而不知道元组的第一个元素的出现次数?
我想要这样的东西:
(A, 5), (B, 5), (C, 3)
.
使用groupby
和itemgetter
:
from itertools import groupby
from operator import itemgetter
from statistics import mean
s = [('A', 1), ('B', 2), ('C', 3), ('A', 9), ('B', 8)]
s2 = sorted(s, key=itemgetter(0)) # sorting the tuple based on 0th index
print([(k, int(mean(list(zip(*g))[1]))) for k, g in groupby(s2, itemgetter(0))])
输出:
[('A', 5), ('B', 5), ('C', 3)]
from collections import defaultdict
sample = [("A", 1), ("B", 2), ("C", 3), ("A", 9), ("B", 8)]
store_alphabet_count = defaultdict(list)
for alphabet, count in sample:
store_alphabet_count[alphabet].append(count)
result = [
(key, sum(value) // len(value)) for key, value in store_alphabet_count.items()
]
print(result)
输出:
[('A', 5), ('B', 5), ('C', 3)]