31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
import json
|
||
|
||
# 1. 读取数据
|
||
with open('movies.json', 'r', encoding='utf-8') as f:
|
||
movies = json.load(f)
|
||
|
||
# 2. ① 找出评分最高和最低的电影
|
||
max_rating_movie = max(movies, key=lambda x: x['rating'])
|
||
min_rating_movie = min(movies, key=lambda x: x['rating'])
|
||
print(f"评分最高电影: {max_rating_movie['title']} - {max_rating_movie['rating']}")
|
||
print(f"评分最低电影: {min_rating_movie['title']} - {min_rating_movie['rating']}")
|
||
|
||
# 3. ② 统计各类型的电影数量 (字典格式)
|
||
genre_counts = {}
|
||
for movie in movies:
|
||
genre = movie['genre']
|
||
genre_counts[genre] = genre_counts.get(genre, 0) + 1
|
||
print("各类型电影数量统计:")
|
||
print(json.dumps(genre_counts, ensure_ascii=False, indent=4))
|
||
|
||
# 4. ③ 统计各导演的电影数量 (字典格式)
|
||
director_counts = {}
|
||
for movie in movies:
|
||
director = movie['director']
|
||
director_counts[director] = director_counts.get(director, 0) + 1
|
||
print("各导演电影数量统计:")
|
||
print(json.dumps(director_counts, ensure_ascii=False, indent=4))
|
||
|
||
# 5. ④ 统计2020年(含)以后上映的电影数量
|
||
count_post_2020 = sum(1 for movie in movies if movie['year'] >= 2020)
|
||
print(f"2020年(含)以后上映的电影数量: {count_post_2020}") |