From 011c34f0becad27eae8cdaff585eea0fe5f66e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E9=9F=B6=E5=9D=A4?= <2509165013@student.example.com> Date: Thu, 25 Jun 2026 16:33:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1.py | 19 +++++++++++++++++++ 2.py | 17 +++++++++++++++++ 3.py | Bin 0 -> 1024 bytes 4.py | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 1.py create mode 100644 2.py create mode 100644 3.py create mode 100644 4.py diff --git a/1.py b/1.py new file mode 100644 index 0000000..dffbf6f --- /dev/null +++ b/1.py @@ -0,0 +1,19 @@ +import json +import matplotlib.pyplot as plt + +movies = json.load(open('movies.json', encoding='utf-8'))['movies'] +# print(movies) +genres = {} +for m in movies: + g = m['genre'] + if g in genres: + genres[g] = genres[g] + 1 + else: + genres[g] = 1 +print(genres) +plt.figure(figsize=(8,5)) +plt.bar(genres.keys(), genres.values()) +plt.title('类型电影数量分布') +plt.xlabel('类型') +plt.ylabel('数量') +plt.savefig('q4_1_bar.png', dpi=150) \ No newline at end of file diff --git a/2.py b/2.py new file mode 100644 index 0000000..360c462 --- /dev/null +++ b/2.py @@ -0,0 +1,17 @@ +import json +import matplotlib.pyplot as plt + +movies = json.load(open('movies.json', encoding='utf-8'))['movies'] +# print(movies) +ratings = [] +durations = [] +for m in movies: + ratings.append(m['rating']) + durations.append(m['duration']) + +plt.figure(figsize=(8,5)) +plt.scatter(durations,ratings,color='red',alpha=0.6) +plt.title('时长与评分关系散点图') +plt.xlabel('duration') +plt.ylabel('rating') +plt.savefig('q4_2_scatter.png', dpi=150) \ No newline at end of file diff --git a/3.py b/3.py new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001 diff --git a/4.py b/4.py new file mode 100644 index 0000000..436ed44 --- /dev/null +++ b/4.py @@ -0,0 +1,38 @@ +import json + +# 读取生成好的电影文件 +with open("movies.json", "r", encoding="utf-8") as f: + data = json.load(f) +movie_list = data["movies"] + +if len(movie_list) == 0: + print("❌ 未抓取到任何电影数据,请先运行pachong.py!") +else: + # ① 找出评分最高、最低电影 + sorted_movies = sorted(movie_list, key=lambda x: x["rating"]) + lowest_movie = sorted_movies[0] + highest_movie = sorted_movies[-1] + print("\n① 评分最高&最低电影:") + print(f"评分最低:{lowest_movie['title']} {lowest_movie['rating']}") + print(f"评分最高:{highest_movie['title']} {highest_movie['rating']}") + + # ② 统计各类型电影数量(字典输出) + genre_count = {} + for m in movie_list: + g = m["genre"] + genre_count[g] = genre_count.get(g, 0) + 1 + print("\n② 各类型电影数量:", genre_count) + + # ③ 统计各导演电影数量(字典输出) + director_count = {} + for m in movie_list: + d = m["director"] + director_count[d] = director_count.get(d, 0) + 1 + print("\n③ 各导演电影数量:", director_count) + + # ④ 统计2020年(含)以后上映电影 + count_2020 = 0 + for m in movie_list: + if m["year"] >= 2020: + count_2020 += 1 + print(f"\n④ 2020年(含)后上映电影总数:{count_2020}") \ No newline at end of file