[英]Character Frequency from file in Python
so I have this code that can give me the frequency of all the letters in a file, but I would like to find the frequency of every 5th letter. 所以我有这段代码可以给我文件中所有字母的频率,但是我想找到每5个字母的频率。 Can somebody help me out? 有人可以帮我吗?
import collections
import string
def translator(frm='', to='', delete='', keep=None):
# Python Cookbook Recipe 1.9
# Chris Perkins, Raymond Hettinger
if len(to) == 1: to = to * len(frm)
trans = string.maketrans(frm, to)
if keep is not None:
allchars = string.maketrans('', '')
# delete is expanded to delete everything except
# what is mentioned in set(keep)-set(delete)
delete = allchars.translate(allchars, keep.translate(allchars, delete))
def translate(s):
return s.translate(trans, delete)
return translate
alpha = 'abcdefghijklmnopqrstuvwxyz'
keep_alpha=translator(keep=alpha)
while True:
speech = raw_input("Enter file name:")
wholeFile = open(speech, 'r+').read()
lowlet = wholeFile.lower()
letters = keep_alpha(lowlet)
n = len(letters)
occurrences = collections.defaultdict(int)
for x in letters:
occurrences[x]+=1
for x in occurrences:
print x, occurrences[x]
Use a dictionary 使用字典
#make the dictionary
char_dict = {}
#open the file
with open('my_file.txt') as the_data:
#read the data in as a string
the_data_string = the_data.read()
#loop over every 5th character in the string using slicing
for each in the_data_string[::5]:
#try add +1 to the key that the character is located in
try:
char_dict[each] += 1
#if the key doesn't exist, make a new key with the value of 1
except KeyError:
char_dict[each] = 1
You you can then read out just they keys as a list using list(char_dict.keys())
or just the values with list(char_dict.values())
您可以使用,然后刚读出他们键作为一个名单list(char_dict.keys())
或刚好与值list(char_dict.values())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.