Python Forum
Character Count HW | Need suggestions for optimization
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Character Count HW | Need suggestions for optimization
#2
to get character count:
from collections import Counter

def CharacterCount(S):
    S = S.strip().replace(' ','')
    return Counter(S)

print(f"\nCounts 'Python is great fun': {CharacterCount('Python is great fun')}")
print(f"Counts 'Good Times': {CharacterCount('Good Times')}\n")
Output:
Counts 'Python is great fun': Counter({'t': 2, 'n': 2, 'P': 1, 'y': 1, 'h': 1, 'o': 1, 'i': 1, 's': 1, 'g': 1, 'r': 1, 'e': 1, 'a': 1, 'f': 1, 'u': 1}) Counts 'Good Times': Counter({'o': 2, 'G': 1, 'd': 1, 'T': 1, 'i': 1, 'm': 1, 'e': 1, 's': 1})
Reply


Messages In This Thread
RE: Character Count HW | Need suggestions for optimization - by Larz60+ - Nov-25-2020, 08:47 AM

Forum Jump:

User Panel Messages

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