the-honk/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/Letter Frequency.py

18 lines
368 B
Python
Raw Normal View History

2024-10-09 17:02:47 +00:00
from typing import Dict
import re
def letterFrequency(text: str) -> Dict[str, int]:
output = {}
text = re.sub('[^a-zA-Z]+', '', text)
for letter in text:
if letter in output:
output[letter] += 1
else:
output[letter] = 1
return output
frequency = letterFrequency('I wish I wish with all my heart to fly with dragons in a land apart')
print(frequency)