count = [0 for i in range(10)]
for line in text:
line = int(line.strip())-1
index = line / 10
count[index] += 1
print count
#=> 结果
[2, 0, 0, 1, 0, 0, 0, 1, 0, 0]
这样正好就第一位是1-10, 第四位是31-40.。
text是你从文件中读取的内容。这个用open和readlines就可以完成了。
其中line = int(line.strip())-1是为了将每个index中的检测范围向上扩1,即本身第一位是0-9的,数字-1后,就能将1-10算入第一个中了。
写一个文本统计的脚本:计算并打印有关文本文件的统计数据,包括文件里包含多少个字符、行、单词数,以及前10个出现次数最多的单词按顺序排列import time
keep=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ','-',"'"]
stop_words=['the','and','i','to','of','a','you','my','that','in','she','he','her','his','it','be','was','had']
def normalize(s):
result=''
for c in s.lower():
if c in keep:
result+=c