我正在使用自動填充文本編輯器。當用戶按下空格時,它會接收用戶的輸入並打印帶有用戶提到的前綴的單詞列表。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」兩個字符開頭的單詞。這是你想要的嗎? –