我对 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_common(number) 中更改括号中的数字,但我不想更多地显示其他字母每次显示多少次。
感谢所有的帮助!