Files

32 lines
948 B
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:
data = json.load(f)
movies = data["movies"]
# 1.① 评分最高和最低
max_rating_movie = max(movies, key=lambda x: x["rating"])
min_rating_movie = min(movies, key=lambda x: x["rating"])
print(max_rating_movie["title"], max_rating_movie["rating"])
print(min_rating_movie["title"], min_rating_movie["rating"])
# 2.② 各类型电影数量
genre_count = {}
for movie in movies:
genre = movie["genre"]
genre_count[genre] = genre_count.get(genre, 0) + 1
print(genre_count)
# 3.③ 各导演电影数量
director_count = {}
for movie in movies:
director = movie["director"]
director_count[director] = director_count.get(director, 0) + 1
print(director_count)
# 4.④ 2020年以后电影数量
count_2020_later = 0
for movie in movies:
if movie["year"] >= 2020:
count_2020_later += 1
print(count_2020_later)