我想知道如何在txt文件中查找“ Toal”字符总数,然后使用该数字将其分成txt文件中每个字母的出现次数。我在开头,结尾和总数中都有最常用的字母的编号。任何帮助表示赞赏。谢谢。
import collections
def go (filename):
x=collections.Counter()
with open (filename, encoding= "latin-1") as f:
for line in f:
for word in line.split():
word=word.lower().strip("1234567890[].,?!'][\}{//%^&#@$%^&*()_-|~`,")
if word:
x[word[0]]+=1
return x.most_common()
def lastletter (filename):
x=collections.Counter()
with open (filename, encoding= "latin-1") as f:
for line in f:
for word in line.split():
word=word.lower().strip("1234567890[].,?!'][\}{//%^&#@$%^&*()_-|~`,")
if word:
x[word[-1]]+=1
return x.most_common()
def all (filename):
x=collections.Counter()
with open (filename, encoding= "latin-1") as f:
for line in f:
for word in line.split():
word=word.lower().strip("1234567890[].,?!'][\}{//%^&#@$%^&*()_-|~`,")
for character in word:
x[character]+=1
return x.most_common()