Files
simulated-examination/4.py
2026-06-25 17:09:42 +08:00

38 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
# 读取生成好的电影文件
with open("movies.json", "r", encoding="utf-8") as f:
data = json.load(f)
movie_list = data["movies"]
if len(movie_list) == 0:
print("❌ 未抓取到任何电影数据请先运行pachong.py")
else:
# ① 找出评分最高、最低电影
sorted_movies = sorted(movie_list, key=lambda x: x["rating"])
lowest_movie = sorted_movies[0]
highest_movie = sorted_movies[-1]
print("\n① 评分最高&最低电影:")
print(f"评分最低:{lowest_movie['title']} {lowest_movie['rating']}")
print(f"评分最高:{highest_movie['title']} {highest_movie['rating']}")
# ② 统计各类型电影数量(字典输出)
genre_count = {}
for m in movie_list:
g = m["genre"]
genre_count[g] = genre_count.get(g, 0) + 1
print("\n② 各类型电影数量:", genre_count)
# ③ 统计各导演电影数量(字典输出)
director_count = {}
for m in movie_list:
d = m["director"]
director_count[d] = director_count.get(d, 0) + 1
print("\n③ 各导演电影数量:", director_count)
# ④ 统计2020年以后上映电影
count_2020 = 0
for m in movie_list:
if m["year"] >= 2020:
count_2020 += 1
print(f"\n④ 2020年(含)后上映电影总数:{count_2020}")