Files

34 lines
1.1 KiB
Python
Raw Permalink 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:
total_data = json.load(f)
movie_list = total_data["movies"]
# ① 最高分、最低分电影
max_movie = max(movie_list, key=lambda x: x["rating"])
min_movie = min(movie_list, key=lambda x: x["rating"])
print("评分最高电影:", max_movie["title"], ",评分:", max_movie["rating"])
print("评分最低电影:", min_movie["title"], ",评分:", min_movie["rating"])
# ② 统计各电影类型数量
genre_dict = {}
for m in movie_list:
g_list = m["genre"].split(",")
for g in g_list:
g = g.strip()
genre_dict[g] = genre_dict.get(g, 0) + 1
print("\n各类型电影数量:", genre_dict)
# ③ 统计各导演电影数量
director_dict = {}
for m in movie_list:
name = m["director"]
director_dict[name] = director_dict.get(name, 0) + 1
print("\n各导演电影数量:", director_dict)
# ④ 统计2020年后上映影片
cnt = 0
for m in movie_list:
if m["year"] >= 2020:
cnt += 1
print("\n2020年以后上映电影总数", cnt)