操作计数器信息-Python 2.7

操作计数器信息-Python 2.7,python,collections,counter,Python,Collections,Counter,我对Python相当陌生,我有一个我正在修补的程序。它应该从输入中获取一个字符串,并显示哪个字符是最常见的 stringToData = raw_input("Please enter your string: ") # imports collections class import collections # gets the data needed from the collection letter, count = collections.Counter(stringT

我对Python相当陌生,我有一个我正在修补的程序。它应该从输入中获取一个字符串,并显示哪个字符是最常见的

stringToData = raw_input("Please enter your string: ")
    # imports collections class
import collections
    # gets the data needed from the collection
letter, count = collections.Counter(stringToData).most_common(1)[0]
    # prints the results
print "The most frequent character is %s, which occurred %d times." % (
letter, count)
但是,如果字符串中每个字符有1个,则只显示一个字母,并表示它是最常见的字符。我曾考虑过更改most_commonnumber中括号中的数字,但我不想更多地显示每次其他字母的次数


谢谢你的帮助

正如我在评论中解释的:


您可以将参数保留为most_common,以获得所有字符的列表,按从最常见到最不常见的顺序排列。然后,只要计数器值仍然相同,就只需循环该结果并收集字符。这样您就可以获得所有最常见的字符

从计数器返回n个最常见的元素。或者,如果未指定n,它将返回计数器中按计数排序的所有元素

>>> collections.Counter('abcdab').most_common()
[('a', 2), ('b', 2), ('c', 1), ('d', 1)]
您可以使用此行为简单地循环所有元素,按其计数排序。只要计数与输出中第一个元素的计数相同,您就知道该元素在字符串中仍然以相同的数量出现

>>> c = collections.Counter('abcdefgabc')
>>> maxCount = c.most_common(1)[0][1]

>>> elements = []
>>> for element, count in c.most_common():
        if count != maxCount:
            break
        elements.append(element)
>>> elements
['a', 'c', 'b']

>>> [e for e, c in c.most_common() if c == maxCount]
['a', 'c', 'b']

您可以将参数保留为most_common,以获得所有字符的列表,按从最常见到最不常见的顺序排列。然后,只要计数器值仍然相同,就只需循环该结果并收集字符。这样你就可以得到所有最常见的字符。所以把最常见的字符上的1去掉?我现在如何使用最常用的工具访问该列表?