上传文件至 q2_1_crawler
This commit is contained in:
31
q2_1_crawler/q2_2.py
Normal file
31
q2_1_crawler/q2_2.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import json
|
||||
from collections import Counter
|
||||
|
||||
def analyze_movies(json_path="movies.json"):
|
||||
with open(json_path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
movies = data["movies"]
|
||||
|
||||
# ① 评分最高和最低
|
||||
max_movie = max(movies, key=lambda x: x['rating'])
|
||||
min_movie = min(movies, key=lambda x: x['rating'])
|
||||
print(f"最高评分电影:{max_movie['title']},评分:{max_movie['rating']}")
|
||||
print(f"最低评分电影:{min_movie['title']},评分:{min_movie['rating']}")
|
||||
|
||||
# ② 各类型电影数量
|
||||
genre_counter = Counter()
|
||||
for movie in movies:
|
||||
for genre in movie['genre']:
|
||||
genre_counter[genre] += 1
|
||||
print("各类型电影数量:", dict(genre_counter))
|
||||
|
||||
# ③ 各导演电影数量
|
||||
director_counter = Counter(movie['director'] for movie in movies)
|
||||
print("各导演电影数量:", dict(director_counter))
|
||||
|
||||
# ④ 2020年(含)以后上映的电影数量
|
||||
count_2020_plus = sum(1 for movie in movies if movie['year'] >= 2020)
|
||||
print(f"2020年(含)以后上映的电影数量:{count_2020_plus}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
analyze_movies()
|
||||
Reference in New Issue
Block a user