首页 > 解决方案 > 打印python文件中每个字符的统计信息

问题描述

我要做的是获取文件的数据并打印出文件中每个字符的百分比,但我不想使用重复项。我只需要打印一个具有相关百分比的字符。下面是片段。

for all_char in text:
    char_counter = 0 
    if count_char(text, all_char) > 1:
        perc1 = 100 / len(text) * count_char(text, all_char)
        print("{0} - {1}%".format(all_char, round(perc1, 2)))
        with open(filename, "w") as w:        #<-------- I need a code to remove a single character
            w.truncate(char_counter)
            char_counter += 1

    elif count_char(text, all_char) == 1:
        perc2 = 100 * count_char(text, all_char) / len(text)
        print("{0} - {1}%".format(all_char, round(perc2, 2)))
        char_counter += 1

上面我创建了一个变量char_counter,在每次迭代后都会增加,调用的函数count_char会告诉每个字符在文件中使用了多少次,如果该数字大于 1,则必须从文件中删除字符意味着它将打印只有一次。这是基本想法,但代码给了我一个错误。

标签: pythonpython-3.x

解决方案


您可以通过在字符上使用 a 来获取整个文件的Counter字符数。那么每个字符的百分比是count for that character/total count

from collections import Counter
from itertools import chain

with open(filename) as f:
    counts = Counter(chain.from_iterable(f))

total = sum(counts.values())

for character, count in counts.items():
    print('{:<2} - {:>6.2f}%'.format(repr(character)[1:-1], (count/total) * 100))

对于文本

Mary had a little lamb.

这打印

M  -   4.17%
a  -  16.67%
r  -   4.17%
y  -   4.17%
   -  16.67%
h  -   4.17%
d  -   4.17%
l  -  12.50%
i  -   4.17%
t  -   8.33%
e  -   4.17%
m  -   4.17%
b  -   4.17%
.  -   4.17%
\n -   4.17%

推荐阅读