Python实验四
实验 4:列表与字典应用
目的 :熟练操作组合数据类型。 试验任务:
1. 基础:生日悖论分析。如果一个房间有 23 人或以上,那么至少有两 个人的生日相同的概率大于 50%。编写程序,输出在不同随机样本数 量下,23 个人中至少两个人生日相同的概率。
import random
import matplotlib.pyplot as pltdef birthday_paradox(num_people, num_trials=10000):same_birthday_count = 0for _ in range(num_trials):birthdays = [random.randint(1, 365) for _ in range(num_people)]if len(set(birthdays)) < num_people:same_birthday_count += 1return same_birthday_count / num_trialsdef analyze_birthday_paradox():num_people = 23 probabilities = []for sample_size in range(2, 51): prob = birthday_paradox(sample_size)probabilities.append(prob)if sample_size == num_people:print(f"在 {num_people} 人中,至少有两个人生日相同的概率: {prob:.4f}")plt.plot(range(2, 51), probabilities, marker='o')plt.xlabel('人数')plt.ylabel('概率')plt.title('生日悖论概率')plt.grid(True)plt.show()if __name__ == "__main__":print("分析生日悖论...")analyze_birthday_paradox()
运行截图:
2. 进阶:统计《一句顶一万句》文本中前 10 高频词,生成词云。
import jieba
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as pltdef generate_word_cloud(file_path):with open(file_path, 'r', encoding='gbk') as file:text = file.read()words = jieba.lcut(text)word_counts = Counter(words)top_words = word_counts.most_common(10)print("\n前 10 高频词:")for word, count in top_words:print(f"{word}: {count}")wordcloud = WordCloud(font_path='simhei.ttf', background_color='white',width=800,height=400).generate_from_frequencies(word_counts)plt.figure(figsize=(10, 5))plt.imshow(wordcloud, interpolation='bilinear')plt.axis('off')plt.title('《一句顶一万句》词云')plt.show()if __name__ == "__main__":print("生成《一句顶一万句》的词云...")generate_word_cloud('yi_ju_ding_wan_ju.txt')
运行截图:
3. 拓展:金庸、古龙等武侠小说写作风格分析。输出不少于 3 个金庸(古 龙)作品的最常用 10 个词语,找到其中的相关性,总结其风格。
import jieba
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as pltdef analyze_wuxia_style(author, file_paths):all_words = []for file_path in file_paths:with open(file_path, 'r', encoding='utf-8') as file:text = file.read()words = jieba.lcut(text)all_words.extend(words)word_counts = Counter(all_words)top_words = word_counts.most_common(10)print(f"\n{author} 的前 10 高频词:")for word, count in top_words:print(f"{word}: {count}")wordcloud = WordCloud(font_path='simhei.ttf', background_color='white',width=800,height=400).generate_from_frequencies(word_counts)plt.figure(figsize=(10, 5))plt.imshow(wordcloud, interpolation='bilinear')plt.axis('off')plt.title(f'{author} 的写作风格词云')plt.show()if __name__ == "__main__":print("分析金庸的写作风格...")jinyong_files = ['example_jinyong.txt'] analyze_wuxia_style('金庸', jinyong_files)print("\n分析古龙的写作风格...")gulong_files = ['example_gulong.txt'] analyze_wuxia_style('古龙', gulong_files)
运行截图: