python - 打印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,则必须从文件中删除字符意味着它将打印只有一次。这是基本想法,但代码给了我一个错误。
解决方案
您可以通过在字符上使用 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%
推荐阅读
- javascript - 有条件地映射对象数组
- java - 如何在动态 Web 应用程序中使用 Gecko 驱动程序
- python - matplotlib 中相同 x-tick 标签内的不同字体大小
- magento-2.3 - Magento 在添加优惠券代码时显示错误的折扣?
- vue.js - booststrap-vue 中带有 RTL 的 Vuexy 问题?
- sql-server - 在 SQL Server 中创建一个 json,其中一列作为键,另一列作为值
- python - 熊猫重复索引在满足条件的 groupby 上删除行
- python - 如何从 Plotly 散点图中检索标签信息,仅包含我用套索或缩放突出显示的图中的样本(点)
- python-3.x - Python - 删除不必要的进程后程序变慢
- python - 从用户 discord.py 中删除每个角色