【问题标题】:Binary Search using a for loop, searching for words in a list and comparing使用 for 循环进行二分搜索,在列表中搜索单词并进行比较
【发布时间】:2018-12-16 03:05:26
【问题描述】:

我正在尝试将“alice_list”中的单词与“dictionary_list”进行比较,如果在“dictionary_list”中找不到单词,则打印它并说它可能拼写错误。我遇到的问题是如果找不到它就不会打印任何东西,也许你们可以帮助我。我将“alice_list”附加到大写字母,因为“dictionary_list”全部大写。任何有关它为什么不起作用的帮助将不胜感激,因为我即将把头发拉出来!

       import re
    # This function takes in a line of text and returns
    # a list of words in the line.

    def split_line(line):
        return re.findall('[A-Za-z]+(?:\'[A-Za-z]+)?', line)
    # --- Read in a file from disk and put it in an array.

    dictionary_list = []
    alice_list = []
    misspelled_words = []

    for line in open("dictionary.txt"):
        line = line.strip()
        dictionary_list.extend(split_line(line))

    for line in open("AliceInWonderLand200.txt"):
        line = line.strip()
        alice_list.extend(split_line(line.upper()))


    def searching(word, wordList):
        first = 0
        last = len(wordList) - 1
        found = False
        while first <= last and not found:
            middle = (first + last)//2
            if wordList[middle] == word:
                found = True
            else:
                if word < wordList[middle]:
                    last = middle - 1
                else:
                    first = middle + 1
        return found


    for word in alice_list:
        searching(word, dictionary_list)

--------- 已编辑的有效代码 ---------- 如果有人遇到同样的问题,请更新一些内容,并使用“for word not in”来仔细检查搜索中输出的内容。

"""-----Binary Search-----"""
# search for word, if the word is searched higher than list length, print
words = alice_list
for word in alice_list:
        first = 0
        last = len(dictionary_list) - 1
        found = False
        while first <= last and not found:
            middle = (first + last) // 2
            if dictionary_list[middle] == word:
                found = True
            else:
                if word < dictionary_list[middle]:
                    last = middle - 1
                else:
                    first = middle + 1
                if word > dictionary_list[last]:
                    print("NEW:", word)

# checking to make sure words match
for word in alice_list:
    if word not in dictionary_list:
        print(word)

【问题讨论】:

    标签: python algorithm search binary-search


    【解决方案1】:

    您的函数split_line() 返回一个列表。然后,您将函数的输出附加到字典列表中,这意味着字典中的每个条目都是单词的list,而不是单个单词。快速修复它以使用extend 而不是append

        dictionary_list.extend(split_line(line))
    

    这里的集合可能比列表更好,那么你就不需要二分查找了。

    --编辑--
    要打印不在列表中的单词,只需根据您的函数是否返回 False 过滤列表。比如:

    notfound = [word for word in alice_list if not searching(word, dictionary_list)]
    

    【讨论】:

    • 谢谢!我更新了我的代码,你能看看我如何打印出不在列表中的内容吗?
    • @RocktheFries 请参阅上面的小编辑。您应该能够构建一个 searching() 返回 false 的单词列表。
    【解决方案2】:

    您是否需要对该程序使用二进制搜索? Python 有一个方便的运算符,称为“in”。给定一个元素作为第一个操作数和一个列表/集合/字典/元组作为第二个操作数,如果该元素在结构中,则返回 True,否则返回 false。

    例子:

    1 in [1, 2, 3, 4] -> True
    "APPLE" in ["HELLO", "WORLD"] -> False
    

    因此,对于您的情况,大部分脚本可以简化为:

    for word in alice_list:
        if word not in dictionary_list:
            print(word)
    

    这将打印不在字典列表中的每个单词。

    【讨论】:

    • 我需要使用二进制来显示我对它的了解,但我坚持如何打印它找不到的东西,而不是如果它找到了
    • 如果字典已排序,二分查找比in 快得多,in 假定没有排序并检查每个元素。
    • @ggorlen 你知道如何返回在我的搜索中找不到的名称吗?
    • 我不明白——如果你对某些东西进行二分搜索,但什么也没有出现,你就知道你没有找到它并且可以返回它。或者您可以将最近的项目返回到该项目应该所在的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2016-02-15
    • 2015-07-29
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多