Python 使用计数器时优化输出?
在我计算了文件中的所有符号之后,我正在尝试优化我的输出。例如,我只想打印多次出现的符号Python 使用计数器时优化输出?,python,python-3.x,Python,Python 3.x,在我计算了文件中的所有符号之后,我正在尝试优化我的输出。例如,我只想打印多次出现的符号 from codecs import open as co from collections import Counter with co('test.txt', 'r', 'utf-8', 'strict') as fp: text = fp.read() for char, count in Counter(text).most_common(): if not char.isspace():
from codecs import open as co
from collections import Counter
with co('test.txt', 'r', 'utf-8', 'strict') as fp:
text = fp.read()
for char, count in Counter(text).most_common():
if not char.isspace():
print(char, count)
到目前为止,我的输出:
c102
a 1
b 1
我很乐意提供任何提示或解决方案,特别是如果很容易跟进的话。简单的解决方案是:
for char, count in Counter(text).most_common():
if not char.isspace() and count > 1:
print(char, count)
简单的解决办法是:
for char, count in Counter(text).most_common():
if not char.isspace() and count > 1:
print(char, count)