python常用单词汇总_在.txt文件中找到最常用单词的Python程序必须打印word及其连接...
现在,我有一个函数来替换countChars函数def countWords(lines):
wordDict = {}
for line in lines:
wordList = lines.split()
for word in wordList:
if word in wordDict: wordDict[word] += 1
else: wordDict[word] = 1
return wordDict
但当我运行这个程序时,它会吐出这种讨厌的东西(这只是一个例子,大约有两页文字旁边有大量的数字)before 1478
battle-field 1478
as 1478
any 1478
altogether 1478
all 1478
ago 1478
advanced. 1478
add 1478
above 1478
虽然很明显这意味着代码足够运行,但我没有得到我想要的。
它需要打印文件中每个单词的次数(gb.txt,这是葛底斯堡地址)
很明显文件中的每个单词都不在1478次。。
我对编程很陌生,所以我有点困惑。。from __future__ import division
inputFileName = 'gb.txt'
def readfile(fname):
f = open(fname, 'r')
s = f.read()
f.close()
return s.lower()
def countChars(t):
charDict = {}
for char in t:
if char in charDict: charDict[char] += 1
else: charDict[char] = 1
return charDict
def findMostCommon(charDict):
mostFreq = ''
mostFreqCount = 0
for k in charDict:
if charDict[k] > mostFreqCount:
mostFreqCount = charDict[k]
mostFreq = k
return mostFreq
def printCounts(charDict):
for k in charDict:
#First, handle some chars that don't show up very well when they print
if k == '\n': print '\\n', charDict[k] #newline
elif k == ' ': print 'space', charDict[k]
elif k == '\t': print '\\t', charDict[k] #tab
else: print k, charDict[k] #Normal character - print it with its count
def printAlphabetically(charDict):
keyList = charDict.keys()
keyList.sort()
for k in keyList:
#First, handle some chars that don't show up very well when they print
if k == '\n': print '\\n', charDict[k] #newline
elif k == ' ': print 'space', charDict[k]
elif k == '\t': print '\\t', charDict[k] #tab
else: print k, charDict[k] #Normal character - print it with its count
def printByFreq(charDict):
aList = []
for k in charDict:
aList.append([charDict[k], k])
aList.sort() #Sort into ascending order
aList.reverse() #Put in descending order
for item in aList:
#First, handle some chars that don't show up very well when they print
if item[1] == '\n': print '\\n', item[0] #newline
elif item[1] == ' ': print 'space', item[0]
elif item[1] == '\t': print '\\t', item[0] #tab
else: print item[1], item[0] #Normal character - print it with its count
def main():
text = readfile(inputFileName)
charCounts = countChars(text)
mostCommon = findMostCommon(charCounts)
#print mostCommon + ':', charCounts[mostCommon]
#printCounts(charCounts)
#printAlphabetically(charCounts)
printByFreq(charCounts)
main()
python常用单词汇总_在.txt文件中找到最常用单词的Python程序必须打印word及其连接...相关推荐
- java按行读取txt文件内容_对txt文件中的内容进行排序
如果您的文件中每行都有单词或术语,则可能需要对其进行排序.Java Arrays.sort是执行此操作的常用功能.Collections.sort()是另一个不错的说法.这是一个示例和代码. 在文件E ...
- 把txt文件中逗号改成空格(python)
简单记录下,希望能帮助到有需要的同学. python把txt文件中逗号改成空格 import sys import math import os import numpy as np import m ...
- 将图片名写入txt文件 根据txt文件中的图片名提取特定图像 将图片插入word
本文讲述3种方法,分别是:1. 将图片名写入txt文件:2. 根据txt文件中的图片名提取特定图像:3. 将图片插入word 将图片名写入txt文件(可将训练集.测试集中图片名分别记录在txt文件中) ...
- python找到一行单词中最长的_python - 查找.txt文件中最长的单词,不带标点符号 - SO中文参考 - www.soinside.com...
我正在做Python文件I / O练习,尽管在尝试查找.txt文件每一行中最长的单词的练习上取得了巨大进步,但我无法摆脱标点符号.] > 这是我的代码:with open("origi ...
- python中 t表示什么_在txt文件中\t表示什么?
我用pandas打开一个txt文件,文件中应该有一个分栏符的地方是\t.在 我在文件里读的是这样的:df=pd.read_csv(r'file.txt') 数据帧如下所示: ^{pr2}$ 我希望它看 ...
- python存储大量数据_如何在文件中密集地存储大量数据?
如果你期望存储一个均匀分布的数字,那么看看疯狂的物理学家的答案.但是,如果您预期存储的大部分是小数字,但需要能够存储一些大的数字,那么这些方案可能也很有用.在 如果只需要考虑长度为255字节或更少字节 ...
- python 文件中搜索字符串_在txt文件中搜索字符串Python
为什么你总是得到True的原因已经给出,所以我只提出另一个建议: 如果你的文件不是太大,你可以读它到一个字符串,只是使用(更容易,通常比每行阅读和检查行更快): if 'blabla' in open ...
- python 对 文件内容 搜索_在txt文件中搜索数据
如果您设计了数据格式,固定宽度的列不是一个很好的格式.但如果你被他们困住了,他们很容易对付.在 首先,要分析数据:addressbook = [] with open('addressbook.txt ...
- python读word文档计算字数,Python统计字数并将结果输入到TXT文件中
import string import pandas as pd def str_count(str): '''找出字符串中的中英文.空格.数字.标点符号个数''' count_en = count ...
最新文章
- DevExpress.XtraReports打印
- ejs文件与html,将HTMLWebpackPlugin与EJS文件一起使用
- SQL Server 2005 MD5函数
- 在一个JS文件中包含中文字符串,通过innerHTML输出后中文乱码?
- java identity_仔细研究Java Identity API
- [luoguP2760] 科技庄园(背包DP)
- 1002-487-3279*北京大学acm-oj-java语言题解
- AngularJS中的表单验证机制
- python list函数使用总结_python——list总结
- (转)AI vs 深度学习 vs 机器学习:人工智能的 12 大应用场景
- web项目目录访问 路径
- git里面的文件怎么删不掉_彻底删除git中没用的大文件
- xcode6.3 ineligible devices解决方法
- shape属性详解使用
- 2008 go server sql 批处理_Transact-SQL批处理
- 数学回味系列之19 - 有趣的数字
- 5G通信设备安装,不仅是技术活,更是一门艺术活!(干货图文大全)
- 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分
- PHP使用socket TCP通信对接富途牛牛
- dns被劫持怎么办,一文说清dns劫持与防御
热门文章
- SAP删除会计科目 OBR2
- sap会计期间打开关闭
- 订书机是怎样发明的?
- delphi内存泄露查找工具之MemProof教程
- The most important part of the body
- linux keepalived 脚本,Linux下 keepalived 的安装和配置
- oracle存储过程循环单列,Oracle存储过程循环语法实例分析
- hive 语法检查_升级Hive3处理语义和语法变更
- mysql cluster cge (commercial)_Mysql 产品分类和版本说明
- 使用Python的随机数模块 random 实现猜数字游戏