I would like to get the first letter with the maximum occurence of a string.
"google" -> g
"azerty" -> a
"bbbaaa" -> b
from collections import OrderedDict
sentence = "google"
d = OrderedDict()
for letter in sentence:
if letter not in d.keys():
d[letter] = sentence.count(letter)
print(max(d, key=d.get)) # g
from collections import Counter
sentence = "bbbaaa"
c = Counter(sentence)
print(c.most_common()) # have 50% chances of printing 'a' rather than 'b'.
The documentation for
collections.OrderedDict actually has a recipe for an
In : from collections import Counter, OrderedDict In : class OrderedCounter(Counter, OrderedDict): ...: pass ...: In : OrderedCounter("google").most_common() Out: 'g'