python第六周项目答案_中国大学 MOOC 课程 《Python 语言程序设计》第六周课后习题...

该博客介绍了如何使用Python处理文本文件,包括读取文本,转换为小写,替换标点符号,提取单词,构建词汇库,并进行词频统计。通过示例展示了如何排除特定词汇并允许用户动态添加排除词汇,最后按频率从高到低打印前若干个词。
摘要由CSDN通过智能技术生成

1、open(“filename”,”rb”).read()与open(“filename”,”r”).read()的区别,

前者读取二进制码文件,后者读ASCII码文件,文本文件一般以ASCII码编写。

2、split函数

split()默认的话 包含所有空字符,“ ” \n \t等等

split(” “)识别 “ ” 没有的话默认在一块

file=open("name.txt","r")

lines=[]

for line in file:

line=line.split()

lines.append(line)

print (lines)

3、

#提取英文文章的单词

#txt是一个读取了整个文本的字符串

txt=open("hamlet.txt","r").read()

#将字母小写

txt=txt.lower()

#将各种标点符号替换成空格

for ch in txt:

if ch == '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':

txt=txt.replace(ch," ")

txt=txt.split()

hamletxt=txt

#------------------------------------

#-----------------------------------

#构件一个excludes的库,排除这个词汇库的内容

ku=['the','and','to','of','you','a','i','my','in','an','on','that','or','this','not']

def excludes():

for word in txt:

if word in ku:

hamletxt[hamletxt.index(word)]=" "

def addku(number):

for n in range(number):

x=input("请输入要在库中增加的词:")

ku.append(x)

def main():

excludes()

#对单词计数

counts={}

for word in hamletxt:

counts[word]=counts.get(word,0)+1

#-----------------------------------

#对统计值从高到低排序

items=list(counts.items())

items.sort(key=lambda x:x[1],reverse=True)

for i in range(1,11):

print ("{0:<15}{1:>10}".format(str(items[i][0]),str(items[i][1])))

main()

while int(input("1(继续)或0(停止):")):

addku(int(input("请输入要在库中增加的词的数量:")))

main()

print (ku)

open("hamlet.txt","r").close()

hamlet 401

it 325

is 318

his 294

with 263

your 242

but 229

for 228

as 217

be 208

1(继续)或0(停止):1

请输入要在库中增加的词的数量:9

请输入要在库中增加的词:”it”

请输入要在库中增加的词:”is”

请输入要在库中增加的词:”his”

请输入要在库中增加的词:”with”

请输入要在库中增加的词:”your”

请输入要在库中增加的词:”but”

请输入要在库中增加的词:”for”

请输入要在库中增加的词:”as”

请输入要在库中增加的词:”be”

hamlet 401

he 204

what 187

have 173

king 157

will 150

so 141

me 139

we 136

do 130

1(继续)或0(停止):1

请输入要在库中增加的词的数量:8

请输入要在库中增加的词:”he”

请输入要在库中增加的词:”what”

请输入要在库中增加的词:”have”

请输入要在库中增加的词:”will”

请输入要在库中增加的词:”so”

请输入要在库中增加的词:”me”

请输入要在库中增加的词:”we”

请输入要在库中增加的词:”do”

hamlet 401

king 157

are 127

horatio 125

him 118

our 118

by 114

if 111

claudius 109

polonius 107

1(继续)或0(停止):1

请输入要在库中增加的词的数量:5

请输入要在库中增加的词:”are”

请输入要在库中增加的词:”him”

请输入要在库中增加的词:”our”

请输入要在库中增加的词:”by”

请输入要在库中增加的词:”if”

hamlet 401

king 157

horatio 125

claudius 109

polonius 107

no 107

lord 106

shall 106

queen 103

they 101

1(继续)或0(停止):1

请输入要在库中增加的词的数量:2

请输入要在库中增加的词:”no”

请输入要在库中增加的词:”they”

hamlet 401

king 157

horatio 125

claudius 109

polonius 107

lord 106

shall 106

queen 103

all 100

good 97

1(继续)或0(停止):1

请输入要在库中增加的词的数量:2

请输入要在库中增加的词:”all”

请输入要在库中增加的词:”good”

hamlet 401

king 157

horatio 125

claudius 109

polonius 107

lord 106

shall 106

queen 103

let 95

thou 94

1(继续)或0(停止):2

请输入要在库中增加的词的数量:2

请输入要在库中增加的词:”let”

请输入要在库中增加的词:”thou”

hamlet 401

king 157

horatio 125

claudius 109

polonius 107

lord 106

shall 106

queen 103

from 94

how 87

1(继续)或0(停止):1

请输入要在库中增加的词的数量:2

请输入要在库中增加的词:”from”

请输入要在库中增加的词:”how”

hamlet 401

king 157

horatio 125

claudius 109

polonius 107

lord 106

shall 106

queen 103

at 87

thy 87

1(继续)或0(停止):1

请输入要在库中增加的词的数量:1

请输入要在库中增加的词:”at”

hamlet 401

king 157

horatio 125

claudius 109

polonius 107

lord 106

shall 106

queen 103

thy 87

lord, 84

1(继续)或0(停止):0

[‘the’, ‘and’, ‘to’, ‘of’, ‘you’, ‘a’, ‘i’, ‘my’, ‘in’, ‘an’, ‘on’, ‘that’, ‘or’, ‘this’, ‘not’, ‘it’, ‘is’, ‘his’, ‘with’, ‘your’, ‘but’, ‘for’, ‘as’, ‘be’, ‘he’, ‘what’, ‘have’, ‘will’, ‘so’, ‘me’, ‘we’, ‘do’, ‘are’, ‘him’, ‘our’, ‘by’, ‘if’, ‘no’, ‘they’, ‘all’, ‘good’, ‘let’, ‘thou’, ‘from’, ‘how’, ‘at’]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值