trie_root = []
list = ['to','toe']
for word in list:
if word == "":
continue
else:
cur_node = trie_root
for letter in word:
if len(cur_node)<1:
x=[letter]
cur_node.append(x)
cur_node=x
else:
for i in cur_node:
if letter == i[0]:
x=[i[0:]]
cur_node=x
break
else:
x=[letter]
cur_node.append(x) # This line inserts the next letter in the trie
cur_node = x
break
cur_node.append('$') # This line terminates the word in the trie
print(trie_root)
但是,当我打印trie_root时,输出为[['t', ['o', '$']]]
输出中“脚趾”中缺少的“e”在哪里?如何在那里添加?
但是,如果我的列表包含以不同字母开头的单词,例如'jar'和'toe',则会给出正确的输出:[['j', ['a', ['r', '$']]], ['t', ['o', ['e', '$']]]]
谢谢阅读!