课后题是配套的参考答案
#e10.1CalHamlet.py
def getText():
txt = open("hamlet.txt", "r").read()
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\]^_"{|}~':
txt = txt.replace(ch, " ") #将文本中特殊字符替换为空格
return txt
hamletTxt = getText()
words = hamletTxt.split()
counts = {}
for word in words:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
the 1086
and 967
to 757
of 675
you 555
a 554
i 551
my 520
in 433
it 419
#e10.2CalHamlet.py
excludes = {"the","and","of","you","a","i","my","in"}
def getText():
txt = open("hamlet.txt", "r").read()
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\]^_"{|}~':
txt = txt.replace(ch, " ") #将文本中特殊字符替换为空格
return txt
hamletTxt = getText()
words = hamletTxt.split()
counts = {}
for word in words:
counts[word] = counts.get(word,0) + 1
for word in excludes:
del(counts[word])
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
to 757
it 419
that 388
ham 358
is 346
not 314
his 304
this 298
with 278
but 273
#e10.3CalThreeKingdoms.py
import jieba
excludes = {}#{"将军","却说","丞相"}
txt = open("三国演义.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1: #排除单个字符的分词结果
continue
else:
counts[word] = counts.get(word,0) + 1
for word in excludes:
del(counts[word])
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(15):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
Building prefix dict from the default dictionary ...
Loading model cost 1.020 seconds.
Prefix dict has been built succesfully.
曹操 953
孔明 836
将军 772
却说 656
玄德 585
关公 510
丞相 491
二人 469
不可 440
荆州 425
玄德曰 390
孔明曰 390
不能 384
如此 378
张飞 358
#e10.4CalThreeKingdoms.py
import jieba
excludes = {"将军","却说","荆州","二人","不可","不能","如此"}
txt = open("三国演义.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)
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 = "曹操"
else:
rword = word
counts[rword] = counts.get(rword,0) + 1
for word in excludes:
del(counts[word])
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(5):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
曹操 1451
孔明 1383
刘备 1252
关羽 784
张飞 358
#6.1
from random import randint
def randd():
pword=''
for i in range(8):
u = randint(0,62)
if u>=10:
if 90<(u+55)<97:
pword+=chr(u+62)
else:
pword+=chr(u+55)
print("{} ".format(u+55),end="")
else:
pword+='%d'%u
return pword
def main():
for i in range(1,11):
print("生成的第{}个密码是:{}".format(i,randdd()))
main()
115 99 65 75 生成的第1个密码是:7scA5K69
100 98 97 76 71 72 81 109 生成的第2个密码是:dbaLGHQm
115 67 67 65 112 104 66 生成的第3个密码是:sCCAphB3
116 113 72 90 75 108 109 生成的第4个密码是:tq9HZKlm
66 114 71 66 80 生成的第5个密码是:6BrG6B2P
89 103 95 114 80 87 生成的第6个密码是:Y3gfrP2W
95 108 111 71 87 115 生成的第7个密码是:floG3Ws0
101 75 81 75 71 生成的第8个密码是:eKQ52K4G
84 77 93 82 107 116 70 生成的第9个密码是:T4MdRktF
84 83 79 77 104 89 89 生成的第10个密码是:TSOM5hYY
#6.2
def main():
num=[]
n=input("请输入一组数字(或者直接按回车结束程序):")
while n!="":
num.append(eval(n))
n=input("请输入一组数字(或者直接按回车结束程序):")
else:
print("正在处理,请稍等")
judge(num)
def judge(n):
if len(n) == len(set(n)):
print("鉴定完毕,没有重复的元素")
else:
print("有重复的元素,总共有{}个".format(len(n)-len(set(n))))
main()
请输入一组数字(或者直接按回车结束程序):56
请输入一组数字(或者直接按回车结束程序):25
请输入一组数字(或者直接按回车结束程序):56
请输入一组数字(或者直接按回车结束程序):22
请输入一组数字(或者直接按回车结束程序):11
请输入一组数字(或者直接按回车结束程序):
正在处理,请稍等
有重复的元素,总共有1个
#6.4
txt=input("请输入您想输入的英文句子:")
counts={}
ex=[',','.','?','!',':','"',';']
for i in txt:
if i == " " or i in ex:
continue
else:
if ord(i)<97:
i=chr(ord(i)+32)
counts[i]=counts.get(i,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for u in range(len(items)):
alpha,count=items[u]
print("{} -> {}".format(alpha,count))
请输入您想输入的英文句子:hsgstge
s -> 2
g -> 2
h -> 1
t -> 1
e -> 1
#6.6
import jieba.posseg as ps
txt = open('红楼梦.txt','r',encoding = 'utf-8').read()
exclude = ['明白']
counts = {}
def countFigures():
words = ps.cut(txt)
for w in words:
if len(w.word) == 1:
continue
if w.flag == 'nr':
counts[w.word] = counts.get(w.word, 0) + 1
for key in exclude:
del(counts[key])
items = list(counts.items())
items.sort(key = lambda x:x[1], reverse = True)
for i in range(20):
word, count = items[i]
print('{0:<10}{1:>5}'.format(word,count))
countFigures()
宝玉 3748
贾母 1252
凤姐 1129
王夫人 1011
老太太 966
黛玉 870
宝钗 747
贾琏 679
凤姐儿 470
薛姨妈 453
贾政 433
探春 432
紫鹃 406
小丫头 287
贾珍 284
邢夫人 284
林黛玉 280
尤氏 267
薛蟠 237
贾蓉 176
标签:count,word,words,Python,items,课后,第六章,counts,txt
来源: /qq_41318400/article/details/89424349
python语言程序设计基础第二版第六章答案-Python语言程序设计基础(第2版) 课后题 第六章...