字符串中的join()方法
t = ",".join(['aaa', 'bbb', 'ccc'])
# join()将列表中的元素用指定的分隔符连接起来,连接成一个字符串
print(t)
将《三国演义》中出场频度高的人物名字,通过图片的形式做成词云(名字越大,人物出场频度越高)
import jieba
import wordcloud # 词云库
import imageio.v2 as imageio # 涉及到图片处理的库
import os # 往文件夹输出库
# 图片中字号越大的人物出场频度越大
# 读取txt文件,获取需要统计词汇的文本
with open("三国演义.txt", "r", encoding="utf-8") as f:
t = f.read()
# 设置图像遮罩,即背景图片(生成词云的背景图片)
mask = imageio.imread("guanyu.jpg")
# 请在下列exludes集合中,自行补充其他需要排除的词汇
excludes = {"将军", "却说", "二人", "不可", "荆州", "不能", "如此", "商议", "如何", "主公", "军士", "左右", "军马",
"引兵", "次日", "大喜", "天下", "东吴", "于是", "今日", "不敢", "魏兵", "陛下", "一人", "人马", "不知",
"汉中", "只见", "众将", "蜀兵", "上马", "大叫", "太守", "此人", "夫人", "先主",
"后人", "背后", "城中", "天子", "一面", "何不", "大军", "忽报", "先生", "百姓", "何故", "然后",
"先锋", "不如", "赶来", "原来", "令人", "江东", "下马", "喊声", "正是", "徐州", "忽然", "因此",
"成都", "不见", "未知", "大败", "大事", "之后", "一军", "引军", "起兵", "军中", "接应", "进兵",
"大惊", "可以", "以为", "大怒", "不得", "心中"
}
words = jieba.lcut(t)
counts = {}
for word in words:
# 去除虚词和标点符号
if len(word) == 1:
continue
# 将同一个人的名字统一
elif word == "玄德" or word == "玄德曰":
rword = "刘备"
elif word == "关公" or word == "云长":
rword = "关羽"
elif word == "孔明曰" or word == "诸葛亮":
rword = "孔明"
elif word == "丞相" or word == "孟德":
rword = "曹操"
elif word == "都督" or word == "公瑾":
rword = "周瑜"
elif word == "翼德" or word == "三将军":
rword = "张飞"
elif word == "刘禅":
rword = "后主"
elif word == "奉先":
rword = "吕布"
elif word == "仲达":
rword = "司马懿"
else:
rword = word
counts[rword] = counts.get(rword, 0) + 1
# 实现删除干扰词汇功能(此处约2行代码)
for word in excludes:
del counts[word]
# 使用列表和lambda功能实现 词汇的排序 (此处约2行代码)
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
# 将列表中的某一个元素,替代名词转换为正式姓名
items[items.index(("孔明", 1383))] = ("诸葛亮", 1383)
items[items.index(("后主", 217))] = ("刘禅", 217)
# 把列表里面的元素用空格分隔开,并连接成一个大的字符串
# dict(items).keys()获取字典中的键,不需要值
# 最后结果中字越大说明词的频度越高
txt = " ".join(dict(items).keys())
# font_path是字体库,生成图片的宽度,高度,背景颜色,作为词云背景的图片
w = wordcloud.WordCloud(font_path="msyh.ttc", width=1000, height=700, background_color="white", mask=mask)
w.generate(txt)
# 调用generate生成图片
# 判断输出文件夹是否存在,如果不存在则创建
outputFileFolder = "output" # 创建一个文件夹,将图片放在文件夹里面
if os.path.exists(outputFileFolder) == False:
os.mkdir(outputFileFolder)
# 输出打印生成的图片到指定文件夹
w.to_file("output\SanGuoWordCloudV1.png")
随机密码生成。编写程序,在26个字母大小写和9个数字组成的列表中随机生成10个8位密码
import random
s = "qwertyuiopasdfghjklzxcvbnm0123456789QWERTYUIOPASDFGHJKLZXCVBNM"
slist = list(s)
mlist = []
for i in range(10):
mm = ''
for j in range(8):
mm += slist[random.randint(0, 61)]
mlist.append(mm)
print(mlist)
生日悖论分析。生日悖论指如果一个房间里有23人或以上,那么至少有两个人生日相同的概率大于50%。编写程序,输出在不同随机样本数量下,23个人中至少两个人生日相同的概率。
import random
counts = 0
for i in range(100):
l = []
for k in range(23):
l.append(random.randint(1, 365))
if len(l)>len(set(l)):
counts+=1
print(counts/100)