我不确定这是否是您想要的,但这会打印出最常见的字符,伴随着事件的数量:
import collections
char, num = collections.Counter(stringToData).most_common(1)[0]
print "In your string, the most frequent character is %s, which occurred %d times" % (char, num)
这返回了最常见的特征和出现数量的元组。
collections.Counter(stringToData).most_common(1)[0]
#output: for example: ('f', 5)
例子:
stringToData = "aaa bbb ffffffff eeeee"
char, num = collections.Counter(stringToData).most_common(1)[0]
print "In your string, the most frequent character is %s, which occurred %d times" % (char, num)
输出为:
In your string, the most frequent character is f, which occurred 8 times