我的程序从字典中创建直方图。它首先从文本文件中读取文本。然后计算并打印出每个单词的长度。例如,单词“in”,“the”和“Florida”会产生这样的计数。 长度计数 2 1 3 1 7 1 因为只有1个长度为2的单词,1个长度为3的单词和1个长度为7的单词。我的文本文件有数百个单词。所以,有一些长度为10或更长的单词。但在水平轴上10后它没有显示任何东西。请帮忙。以下是我的代码。
import sys
import string
def rem_punc(w):
plst = list(string.punctuation)
for p in plst:
if p in w:
w = w.replace(p,'')
return w
def word_length_processor(text):
d = dict()
for w in text:
w = rem_punc(w)
if len(w) > 0:
n = len(w)
if n not in d:
d[n] = 1
else:
d[n] = d[n] + 1
for k,v in d.items():
print("{0} {1}".format(k,v))
print()
hist_maker(d)
def hist_maker(d):
freq = d
for y in range(300,9,-10):
print ("{:>6} | ".format(y), end="")
for x in range(1,10):
if freq.get(x,0) >= y:
column = "***"
else:
column = " "
print(column, end="")
print()
print(" ------------------------------------------------------")
print(" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15")
arg_string = sys.argv[1]
try:
f = open(arg_string, 'r')
except:
print('Could not open file. Please try again.')
sys.exit()
text = f.read()
f.close()
text = text.split()
print("{0} {1}".format("Length","Count"))
print(word_length_processor(text))