#读出文本本件,统计单词数输出;读出文本文件,随机输出其中的10个单词
import re
import random
f=open("this.txt","r")
r = re.compile(r"[,!\*\.]")
#统计每个单词数
words = {}
f=open("this.txt","r")
for line in f:
for word in r.sub("",line.strip()).split(" "):
if word in words:
words[word] += 1
words.setdefault(word,1)
print(words)
#随机输出10个单词
f=open("this.txt","r")
a=[]
for line in f:
for word in r.sub("",line.strip()).split(" "):
a.append(word)
l=[]
for i in range(10):
x=random.randint(0,len(a)-1)
if x not in l:
l.append(x)
for i in range(10):
print(a[l[i]])
f.close()
"this.txt"
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is