上周tensorflow小组作业记录一下,初始代码来自周帜老师我们可爱的gg,我做了一些输出调整,并制作了原理讲解,以下:所见即所得。
操作:把txt文件和.py文件保存在一个文件夹下,接着复制粘贴代码就可以所见即所得啦
思路将在以下的图片中分步讲明,也可以拉到底直接看代码
代码:
#完成词频的统计,需要将dictTxt.txt文件和程序文件放在同一文件夹中
import operator
import os
#打开文件
speech_text=''
try:
f = open(os.path.abspath('dictTxt.txt'),'r')
speech_text = f.read()
f.close()
except:
print("File Open Error.")
print('Speech text:')
print(speech_text)
#将单词转换为小写
speech = speech_text.lower().split()
print('After lower()&split():')
print(speech)
# 利用字典进行处理
dic = {}
for word in speech:
if word not in dic:
dic[word]=1
else:
dic[word]+=1
print('Dic:')
print(dic)
#对字典中的进行排序
swd = sorted(dic.items(),key=operator.itemgetter(1),reverse=True)
print(swd)
我还写了另外一种代码写法,但是没有上一种好,也粘上来一下
#完成词频的统计,需要将dictTxt.txt文件和程序文件放在同一文件夹中
import operator
#打开文件
try:
f = open('dictTxt.txt','r')
speech_text = f.read()
finally:
if f:
f.close()
#将单词转换为小写
speech = speech_text.lower().split()
#print(speech)
# 利用字典进行处理
dic = {}
for word in speech:
if word not in dic:
dic.update({word:1})
else:
dic.update({word:dic[word]+1})
#对字典中的进行排序
swd = sorted(dic.items(),key=operator.itemgetter(1),reverse=True)
print(swd)
p.s.
附上dictTxt.txt文件内容:
My fellow citizens: I stand here today humbled by the task before us, grateful for the trust you’ve bestowed, mindful of the sacrifices borne by our ancestors.
I thank President Bush for his service to our nation – (applause) – as well as the generosity and cooperation he has shown throughout this transition.
Forty-four Americans have now taken the presidential oath. The words have been spoken during rising tides of prosperity and the still waters of peace. Yet, every so often, the oath is taken amidst gathering clouds and raging storms. At these moments, America has carried on not simply because of the skill or vision of those in high office, but because we, the people, have remained faithful to the ideals of our forebears and true to our founding documents.
So it has been; so it must be with this generation of Americans.
That we are in the midst of crisis is now well+++ understood. Our nation is at war against a far-reaching network of violence and hatred. Our economy is badly weakened, a consequence of greed and irresponsibility on the part of some, but also our collective failure to make hard choices and prepare the nat