То, что мне не хватает, это по сути одно и то же.Одним из них является мое решение, другое решение Google, которые оба решают одну и ту же проблему.Однако я продолжаю замечать, что версия Google как-то в два раза быстрее моей.Чего мне не хватает?
#mimic dictionary
"""
match each word in a string to a list of words that will follow it
example:
"hi my name is chris, hi chris, hi how are you"
{hi: ["my","chris","how"], my: ["name"],...}
"""
def mimic_dict(str):
list1 = str.split()
dict = {}
index, end = 0, len(list1) - 1
while index < end:
current, next = list1[index], list1[index + 1]
if not current in dict:
dict[current] = [next]
else:
dict[current].append(next)
index += 1
return dict
#google
def mimic_dict1(text):
mimic_dict = {}
words = text.split()
prev = ''
for word in words:
if not prev in mimic_dict:
mimic_dict[prev] = [word]
else:
mimic_dict[prev].append(word)
prev = word
return mimic_dict