【发布时间】:2020-04-11 00:19:43
【问题描述】:
我编写了一个函数来计算文件中字符的频率。该功能按预期工作,但是,我想按键按字母顺序打印字典。我如何在 Python 中以最简单的方式做到这一点。
这是我的功能:
def characters(textfile):
textfile=open(textfile)
characters={}
for line in textfile:
for char in line:
if char in characters:
characters[char]+=1
else:
characters[char]=1
print(characters)
【问题讨论】:
-
这能回答你的问题吗? How can I sort a dictionary by key?
-
您好!这回答了你的问题了吗? stackoverflow.com/questions/19650836/…
标签: python sorting dictionary