您如何编写代码来计算字符在文件中的出现次数,并将数据放入字典中。
例如, 如果我向代码提供文件,则希望它返回每个新字符作为键,并返回计数作为值。
让我们说文件内部是“ python很酷”
我希望代码返回 {'p':1,'y':1,'t':1,'h':1,'o':3 ...}
我了解如何通过简单地使用以下方法来计算字符串中的字符:
def count_chars(text):
'''
(string) -> int
Returns the length of text.
Examples:
>>> count_chars("simon")
5
>>> count_chars("")
0
'''
count = 0
for ch in text:
count += 1
return count
但是,我正在努力打开文件,然后将数据放入字典中。
答案 0 :(得分:2)
使用随附的collections.Counter
类。 most_common
方法甚至包括用于对字符串的字符进行计数的代码示例:
>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
将其与打开文件一起放置:
from collections import Counter
def count_characters(source_file_name):
with open(source_file_name, 'r') as source_file:
return Counter(source_file.read())
答案 1 :(得分:1)
由于您已经提到这是一项任务,因此我想向您展示如何直接使用字典/不使用Counter来“直接”完成此操作,因此您实际上了解了幕后工作。
d = {} # initialise a dictionary
with open(file_path, 'r') as f: # open the file, call it 'f'
for line in f: # read through each line in the file
for c in line: # read through each character in that line
if c in d: # check if character is in dictionary
d[c] += 1 # if it's been seen before, increment counter
else:
d[c] = 1 # otherwise, insert it into the dictionary
答案 2 :(得分:0)
要满足您的项目要求,只需将Counter
转换为dict
。从亚历克斯的代码中:
from collections import Counter
def count_characters(source_file_name):
with open(source_file_name, 'r') as source_file:
return dict(Counter(source_file.read()))