我正在使用自动填充文本编辑器。当用户按下空格时,它会接收用户的输入并打印带有用户提到的前缀的单词列表。Python函数在传递字符串变量时返回空列表
下面是代码:
#!/usr/bin/env python
from tkinter import *
import tkinter.font as tkFont
class Node:
def __init__(self):
self.word = None
self.nodes = {} # dict of nodes
def __get_all__(self):
x = []
for key, node in self.nodes.items():
if (node.word is not None):
x.append(node.word)
x = x + node.__get_all__
return x
def __str__(self):
return self.word
def __insert__(self, word, string_pos=0):
current_letter = word[string_pos]
if current_letter not in self.nodes:
self.nodes[current_letter] = Node();
if (string_pos + 1 == len(word)):
self.nodes[current_letter].word = word
else:
self.nodes[current_letter].__insert__(word, string_pos + 1)
return True
def __get_all_with_prefix__(self, prefix, string_pos):
x = []
#print("We are in the get prefix func", prefix)
for key, node in self.nodes.items():
if (string_pos >= len(prefix) or key == prefix[string_pos]):
if (node.word is not None):
x.append(node.word)
if (node.nodes != {}):
if (string_pos + 1 <= len(prefix)):
x = x + node.__get_all_with_prefix__(prefix, string_pos + 1)
else:
x = x + node.__get_all_with_prefix__(prefix, string_pos)
return x
class Trie:
def __init__(self):
self.root = Node()
def insert(self, word):
self.root.__insert__(word)
def get_all(self):
return self.root.__get_all__
def get_all_with_prefix(self, prefix, string_pos=0):
return self.root.__get_all_with_prefix__(prefix, string_pos)
root = Tk()
trie = Trie()
customFont = tkFont.Font(family="arial", size=17)
with open('words_file_for_testing.txt', mode='r') as f:
for line in f:
for word in line.split():
trie.insert(word)
def retrieve_input(self):
inputValue = content_text.get("1.0", "end-1c")
print(trie.get_all_with_prefix(inputValue))
printing_the_list(inputValue)
def printing_the_list(getinputvalue):
print(getinputvalue)
print(type(getinputvalue))
print(trie.get_all_with_prefix("A"))
print(trie.get_all_with_prefix(getinputvalue))
#print(type(words))
#print(words)
#print(trie.get_all_with_prefix("A"))
#master = Tk()
#listbox = Listbox(master)
#listbox.pack()
#for item in words:
# listbox.insert(END, item)
root.title("Autocomplete Word")
root.geometry('800x400+150+200')
content_text = Text(root, wrap='word', font=customFont)
content_text.focus_set()
content_text.pack(expand='yes', fill='both')
scroll_bar = Scrollbar(content_text)
content_text.configure(yscrollcommand=scroll_bar.set)
scroll_bar.config(command=content_text.yview)
scroll_bar.pack(side='right', fill='y')
root.bind("<space>", retrieve_input)
root.mainloop()
现在,我有问题,其printing_the_list(getinputvalue)
功能。在此功能中,getinputvalue
是存储用户输入值的变量。当我手动输入字符串到print(trie.get_all_with_prefix("A"))
函数时,它会根据需要打印单词列表,但是,当我尝试使用getinputvalue
变量打印具有用户输入值的单词的前缀列表时,获得了一个空列表[]
。
上面的Python代码打印:
[]
A
<class 'str'>
['AAE', 'AAEE', 'AAG', 'AAF', 'AAP', 'AAPSS', 'AAM', 'AAMSI', 'AARC', 'AAII', 'AAO', 'Aar', 'Aaron', 'Aarika', 'Aargau', 'Aaren', 'Aarhus', 'Aara', 'Aarau', 'Aandahl', 'Aani', 'Aaqbiye', 'Aalesund', 'Aalto', 'Aalborg', 'Aalst', 'Aachen', 'A-and-R']
[]
我在做什么错。
我没有帮助在这里,但只知道'__insert__'和'__get_all__'是**非常糟糕的命名方法**。 '__ __'方法是为内置的python保留的,你不应该这样命名方法或属性。你可以把它命名为'_ ',如果你想使它成为“_private_”(技术上不是,但是这是一个约定) –
Arount
同意,除了一个小点,它更像是:'_protected'和'__private'(按照惯例) – acushner
您的测试输出是否可取?当你得到所有带有前缀“A”的单词时,你只能得到以“Aaron”和“Aalto”两个字符开头的单词。这是你想要的吗? –