上传文件至 q2_1_crawler
This commit is contained in:
32
q2_1_crawler/q2 (2).py
Normal file
32
q2_1_crawler/q2 (2).py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
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)
|
||||||
Reference in New Issue
Block a user