Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用计数器时优化输出?_Python_Python 3.x - Fatal编程技术网

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)