from collections import defaultdict
def reciprocal_rank_fusion(rankings, k=60):
scores = defaultdict(float)
for ranking in rankings:
for rank, item in enumerate(ranking, start=1):
scores[item] += 1 / (k + rank)
return sorted(
scores.items(),
key=lambda x: x[1],
reverse=True
)
teacher = ["恐竜ずかん", "宇宙のひみつ", "海の生き物"]
friends = ["宇宙のひみつ", "昆虫大百科", "恐竜ずかん"]
ai = ["恐竜ずかん", "海の生き物", "宇宙のひみつ"]
rankings = [teacher, friends, ai]
result = reciprocal_rank_fusion(rankings, k=10)
for title, score in result:
print(title, round(score, 3))
出力例
恐竜ずかん 0.259
宇宙のひみつ 0.251
海の生き物 0.160
昆虫大百科 0.083














