【发布时间】:2017-02-20 18:53:11
【问题描述】:
我正在编写一个程序来计算每个字母的输入次数,以帮助我进行频率分析。我的程序有效,但它总是沿曲线输出我的部分答案。示例输出:
Length of message: 591 characters
A 11 1%
B 27 4%
C 37 6%
D 2 0%
E 2 0%
F 5 0%
G 17 2%
H 8 1%
I 9 1%
J 49 8%
L 7 1%
M 44 7%
N 20 3%
P 42 7%
Q 6 1%
R 36 6%
S 1 0%
U 6 1%
V 22 3%
W 13 2%
X 56 9%
Y 11 1%
我正在使用以下代码:
text = input()
symbols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letters = collections.Counter(text.upper())
length = len(text)
print('Length of message: {} characters'.format(length))
for letter, times in sorted(letters.items()):
if letter not in symbols:
continue
percent = str(int((times / length) * 100)) + '%'
print(letter, times, percent)
我试图让它显示如下:
A 11 1%
B 27 3%
C 37 6%
D 2 0%
E 2 0%
F 5 0%
G 17 2%
H 8 1%
I 9 1%
J 49 8%
L 7 1%
M 44 7%
N 20 3%
P 42 7%
Q 6 1%
R 36 6%
S 1 0%
U 6 1%
V 22 3%
W 13 2%
X 56 9%
Y 11 1%
提前谢谢你!
【问题讨论】:
标签: python python-3.x data-visualization frequency-analysis python-3.6