使用计数器时优化输出?

时间:2017-06-06 15:25:27

标签: 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():

    print(char, count) 
到目前为止

我的输出:

c 102
a 1 
b 1

我很高兴任何提示或解决方案,特别是如果它很容易下载。

2 个答案:

答案 0 :(得分:2)

简单的解决方案是:

for char, count in Counter(text).most_common():
  if not char.isspace() and count > 1:
     print(char, count) 

答案 1 :(得分:1)

output = filter(lambda a: a[1] > 1, Counter(text).most_common())
# output = [('c', 102)]
for char, count in output:
    if not char.isspace():
        print(char, count)
相关问题