11.5.2. Practice: Dictionary Counters#
Now it’s your turn to practice dictionary counters
Copy the code from the demo that counts letters in a string. Modify the string to be something else and find the letter_counts (no need to sort)
# TODO: enter your code here
Now let’s try this with words.
The code below makes a string, and then splits it into words by dividing it apart at each space.
# Save a poem into a string (we can use """ to make a multiline string)
# Fire and Ice BY ROBERT FROST (now public domain)
poem = """Some say the world will end in fire,
Some say in ice.
From what I’ve tasted of desire
I hold with those who favor fire.
But if it had to perish twice,
I think I know enough of hate
To say that for destruction ice
Is also great
And would suffice."""
# split the string (all lowercase) into words
import re # import the Regular Expressions library, to help us split words
#make the poem all lowercase
lower_case_poem = poem.lower()
# split the poem into pieces at all spaces and newlines (\s), and ,'s and .'s
poem_split_by_spaces_and_punctuation = re.split(("[\s,.]"), lower_case_poem)
# get rid of some empty strings "" that ended up in our list
split_poem = list(filter(None, poem_split_by_spaces_and_punctuation))
print(split_poem)
['some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire', 'some', 'say', 'in', 'ice', 'from', 'what', 'i’ve', 'tasted', 'of', 'desire', 'i', 'hold', 'with', 'those', 'who', 'favor', 'fire', 'but', 'if', 'it', 'had', 'to', 'perish', 'twice', 'i', 'think', 'i', 'know', 'enough', 'of', 'hate', 'to', 'say', 'that', 'for', 'destruction', 'ice', 'is', 'also', 'great', 'and', 'would', 'suffice']
<>:20: SyntaxWarning: invalid escape sequence '\s'
<>:20: SyntaxWarning: invalid escape sequence '\s'
C:\Users\kmthayer\AppData\Local\Temp\ipykernel_57052\735270539.py:20: SyntaxWarning: invalid escape sequence '\s'
poem_split_by_spaces_and_punctuation = re.split(("[\s,.]"), lower_case_poem)
Make code that counts how often each word appears in the poem (it should be very similar to the code from problem 1 above
# TODO: enter your code here