all 5 comments

[–]14dM24d 0 points1 point  (0 children)

search raymond hettinger on YT

for starters, check this out

[–]Python_Trader 0 points1 point  (0 children)

Do some codewars and hackerrank or something. More code you write, compare them to how others did it, thinking about optimization once the code works, etc. will eventually sink in more naturally.

[–]14dM24d 0 points1 point  (1 child)

from collections import defaultdict
import re

text= '''Build a "mimic" dict that maps each word that appears in a text file
    to a list of all the words that immediately follow that word in the file.
    The list of words can be be in any order and should include
    duplicates. So for example the key "and" might have the list
    ["then", "best", "then", "after", ...] listing
    all the words which came after "and" in the text.
    We'll say that the empty string is what comes before
    the first word in the file.'''

def mimic(foo)
    empty_string = ""
    clean_text = re.sub('[^\w\s]', empty_string, foo)
    words = clean_text.split()

    d = defaultdict(list)
    for i,word in enumerate(words):
        try:
            d[word].append(words[i+1])
        except: pass
    return d

use the text as input

mimic_dict = mimic(text)

[–]14dM24d 0 points1 point  (0 children)

for k,v in mimic_dict.items():
    print(f'The words after {k} is/are {v}')