Files
simulated-examination/q2_1_crawler/q2_2.py
2509165027 74906f0596 finish
2026-06-23 11:22:41 +08:00

37 lines
1.1 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('q2_1_crawler\movies.json', 'r', encoding='utf-8') as f:
movies = json.load(f)
if movies:
sorted_by_rating = sorted(movies, key=lambda x: x['rating'])
lowest_movie = sorted_by_rating[0]
highest_movie = sorted_by_rating[-1]
print("2.1 最高评分电影:")
print(f"电影名: {highest_movie['title']}, 评分: {highest_movie['rating']}")
print("\n最低评分电影:")
print(f"电影名: {lowest_movie['title']}, 评分: {lowest_movie['rating']}")
genre_counts = {}
for movie in movies:
genre = movie['genre']
genre_counts[genre] = genre_counts.get(genre, 0) + 1
print("\n2.2 各类型电影数量字典:")
print(genre_counts)
director_counts = {}
for movie in movies:
director = movie['director']
director_counts[director] = director_counts.get(director, 0) + 1
print("\n2.3 各导演电影数量字典:")
print(director_counts)
count_2020_later = 0
for movie in movies:
if movie['year'] >= 2020:
count_2020_later += 1
print(f"\n2.4 2020年以后上映的电影数量: {count_2020_later}")