Python. Find the alphabet that came out the most in a sentence?

Asked 2 years ago, Updated 2 years ago, 18 views

I just started Python. I'm asking you a question because I'm stuck in the middle of studying. ㅜ<

sentence = 'Mary had a little lamb' print(sentence.count('a'))

I know how to do this simple thing, but what function should I use to get the most frequently written alphabet in this whole thing?

python

2022-09-22 13:13

1 Answers

Use Counter to count how many times each alphabet appears, and then use max to extract the highest value of them.

from collections import Counter

sentence = 'Mary had a little lamb'
counter = Counter(sentence)
print(max(sentence, key=counter.get))


2022-09-22 13:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.