# 《Python语言程序设计基础》嵩天著-第6章程序部分练习题答案

### program practice 6.1 随机密码生成


import random as r
import random as r
a = ["1","2","3","4","5","6","7","8","9","0","q","w","e","r","t","y","u","i","o","p","a","s",\
"d","f","g","h","j","k","l","z","x","c","v","b","n","m","Q","W","E","R","T","Y","U","I",\
"O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"]
def code():
mi=""
global a #全局变量a
for i in range(8):
n = r.randint(0,61) # 从a列表中任取一个字符串
mi = mi + a[n] # 字符串相加得字符串
return mi
def main():
for i in range(1,11):
print("the {} code is {}".format(i,code()))
main()


### program practice 6.2 重复元素判定

# program practice 6.2 重复元素判定
def repeat():
n = []
a = input("enter what you want:")
while a != "":
n.append(a)
a = input("enter what you want:")
else:
if len(n) == len(set(n)): # set可以去除重复值
return False
else:
return True
repeat()


### program practice 6.4 文本字符分析


def txt():
counts = {}
a = input("please enter anything you want: ")
while a != "":
a = input("please enter anything you want: ")
counts[a] = counts.get(a,0) + 1
else:
b = list(counts.items()) #字典转换成列表，元组为元素
b.sort(key=lambda x:x[1], reverse=True)
c = len(b)
for i in range(c):
word, count = b[i]
print ("{0:<10}{1:>5}".format(word, count))
txt()


### program practice 6.5 生日驳论


import random as r
p = 1
def birth(n):
ls = []
for i in range(n):
a = r.randint(1,366) # 这里直接给每个人随机设置了生日
ls.append(a) # 把所有的生日加到一个列表去
if len(ls) != len(set(ls)): # 不相等即代表有生日相同的~
global p
for i in range(n):
p = p*(366-i) # 生日不相同的取法
else:
print("There is no same birth")
return 1-p/366**n #新学的概论统计派上了用场~
birth(90)


### program practice 6.6 红楼梦人物统计

# program practice 6.6 红楼梦人物统计
import jieba as j
excludes = {"什么","一个","我们","那里","如今","你们","起来","这里","说道",
"众人","他们","出来","姑娘","知道","自己","一面","只见","两个",
"怎么","没有","不是","不知","这个","不知","听见","这样","进来",
"告诉","东西","就是","咱们","回来","夫人","大家","只是","所以",
"出去","不敢","这些","只得","丫头","不过","的话","一时","不好",
"鸳鸯","过来","不能","心里","如此","今日","银子","二人","几个",
"答应","还有","罢了","一回","说话","只管","这么","那边","这话",
"外头","打发","自然","那些","今儿","听说","小丫头","屋里","奶奶"} #排除无用词
txt = open("红楼梦.txt", "r", encoding='utf-8').read() # 必须得把文件放在ipynb同源文件夹下才能这样打开
words  = j.lcut(txt) # 分词后某些字符串仍带有标点符号？
counts = {}
for word in words:
if len(word) == 1:  #排除单个字符的分词结果，尽量确保分词有效为人名
continue # 结束，进行下一个
elif word == "王熙凤" or word == "凤姐" or word == "二奶奶" or word== "凤姐儿":
rword = "王熙凤"
elif word == "老太太" or word == "史太君" or word == "老祖宗":
rword = "贾母"
elif word == "宝钗" or word == "宝姐姐" or word == "宝姑娘":
rword = "薛宝钗"
elif word == "太太" or word == "王夫人" :
rword = "王夫人"
elif word == "花珍珠" or word =="袭人":
rword = "花袭人"
elif word == "湘云" or word == "云儿":
rword = "史湘云"
elif word == "黛玉" or word == "林妹妹" or word == "潇湘妃子":
rword = "林黛玉"
elif word == "宝玉" or word == "宝二爷":
rword = "林宝玉"
else:
rword = word
counts[rword] = counts.get(rword,0) + 1 # i不在c内，返回1，在的话返回对应值+1，相当于给每一个i赋值，值即为次数，每次出现可更新
for word in excludes:
del(counts[word])
items = list(counts.items()) #字典转换成列表，元组为元素
items.sort(key=lambda x:x[1], reverse=True) # 定义x函数为取第二个元素即次数
# reverse为逆向排序，x[1]为元组第二个数据即次数，key即为排序参数
for i in range(20):
word, count = items[i] #键值对输出
print ("{0:<10}{1:>5}".format(word, count))


• 37
点赞
• 247
收藏
觉得还不错? 一键收藏
• 打赏
• 8
评论
03-11 2060
01-08
12-26
07-19

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。