I am working on the python mastery example in his course where you try finding the most repeated character. I have the coding itself set up the way it showed in the solution. I decided to mess around with different sentences. The issue I have in the attached example is that spaces are the most repeater, so the value I get is (’ ', 8). How do I remove it from counting the spaces? I tried adding into the if char in charfreq section a statement that said: and not == " ", but get an error message. I tried adding an elif of saying char != " ", and did not work either. Any help would be appreciated.
Maybe this?
sentence = "I went to the store to get some groceries"
charfreq = {}
for char in sentence:
if char != " ":
if char in charfreq:
charfreq[char] += 1
else:
charfreq[char] = 1
charsorted = sorted(charfreq.items(), key=lambda kv:kv[1], reverse=True)
print(charsorted[0])
1 Like
That worked. Thanks!