根据评论查找每部电影的平均分数可以通过以下步骤实现:
下面是一个示例代码:
import statistics
# 电影评论数据
comments = [
{"movie": "电影A", "score": 8},
{"movie": "电影A", "score": 7},
{"movie": "电影B", "score": 9},
{"movie": "电影B", "score": 8},
{"movie": "电影B", "score": 7},
{"movie": "电影C", "score": 6},
{"movie": "电影C", "score": 5},
]
# 存储每部电影的评分
movie_scores = {}
# 遍历评论数据
for comment in comments:
movie = comment["movie"]
score = comment["score"]
# 将评分添加到对应电影的列表中
if movie in movie_scores:
movie_scores[movie].append(score)
else:
movie_scores[movie] = [score]
# 计算每部电影的平均分数
average_scores = {}
for movie, scores in movie_scores.items():
average_scores[movie] = statistics.mean(scores)
# 打印每部电影的平均分数
for movie, score in average_scores.items():
print(f"{movie}的平均分数为:{score}")
以上代码将根据评论查找每部电影的平均分数,并打印出结果。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云