传递字符串变量时,Python函数返回空列表

时间:2017-07-31 16:05:20

标签: python list tkinter

我正在使用自动填充文本编辑器。当按下空格时,它接收来自用户的输入,并打印具有用户提到的前缀的单词列表。

以下是代码:

#!/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']
[]

我做错了什么。

1 个答案:

答案 0 :(得分:1)

您的问题是,当您键入 A 然后按 space

inputValue = content_text.get("1.0", "end-1c")

返回'A '而不是'A'

这是因为content_text.get() adds a new line character位于字符串的末尾。要忽略换行符和空格,请使用:

inputValue = content_text.get("1.0", "end-2c")