Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Maximum Occurrence Letter
#1
Hey Guys, So I have an assignment regarding finding the most letters used in a word. For some reason I can't seem to make it work as the output that I get is based on First Letter. The def function for max_char_count I got from stack overflow if I'm not mistaken. Any help will be appreciated.

This is my Code.

global str1
def main():
    str1 = input("Enter Your Message: ")
    
    print ("The Lenght of Your Message is:", len(str1),)

    print (max_char_count(str1))

    
def max_char_count(str1):
    max_char = ''
    max_count = 0
    for char in (str1):
        count = str1.count(char)
        if count > max_count:
            max_count = count
            max_char = char
        return max_char


main()
This is my output.

Output:
Enter Your Message: dsdss The Lenght of Your Message is: 5 d >>> ================================ RESTART ================================ >>> Enter Your Message: saaa The Lenght of Your Message is: 4 s
Original Def function. I just add the .upper()

def max_char_count(string):
    max_char = ''
    max_count = 0
    for char in set(string):
        count = string.count(char)
        if count > max_count:
            max_count = count
            max_char = char
    return max_char

print(max_char_count('eeFAGAAJeEff'.upper()))
The Output:

Output:
>>> E >>>

Nah Nevermind Got it. The return is a space ahead. I don't know how to delete this thread.
Reply
#2
In your first function return max_char has the wrong indention. It's one level too deep. It returns inside the loop and you get only the count of the first letter.

To count something, you should look for collections.Counter.

import collections
text = 'This is a test. Hello World. Foo Bar Baz...'
c = collections.Counter(text)
print('Most common')
print(c.most_common(3))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Using collections is probably cheating, but using a dict might be fine:
>>> def max_char(string):
...     chars = {string.count(ch): ch for ch in string}
...     most_used = max(chars.keys())
...     return chars[most_used]
...
>>> max_char("foo bar")
'o'
>>> max_char("spam eggs")
's'
>>> max_char("on a bright sunny day in august")
' '
The important part, is that you actually understand how it works, whichever one you turn in.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get unique entries in a list and the count of occurrence james2009 5 2,910 May-08-2022, 04:34 AM
Last Post: ndc85430
  Selecting the first occurrence of a duplicate knight2000 8 5,086 May-25-2021, 01:37 AM
Last Post: knight2000
  Checking for one or more occurrence in a list menator01 3 2,635 May-18-2020, 06:44 AM
Last Post: DPaul
  count occurrence of numbers in a sequence and return corresponding value python_newbie09 6 3,384 May-20-2019, 06:33 PM
Last Post: python_newbie09
  Word co-occurrence matrix for a string (NLP) JoeB 2 11,563 Feb-27-2018, 11:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020