简体   繁体   English

您如何在三重嵌套列表中进行搜索?

[英]How do you search in triple nested lists?

I am trying to search through triple nested lists in python. 我正在尝试搜索python中的三重嵌套列表。 I came up with a super messy way that doesn't work. 我想出了一种超级混乱的方式,这种方式行不通。 How do you do this effectively? 您如何有效地做到这一点? I'm using python 3. The list I'm trying to search through is not nececarily triple nested in every slot. 我正在使用python3。我尝试搜索的列表不一定在每个插槽中都是三层嵌套的。

Here's is the bad messy way I wrote that doesn't work for some reason. 这是我写的一团糟的杂乱方式,由于某种原因它不起作用。

HELLO = ["hello", "hi"]
GOODBYE = ["goodbye", "bye"]

POLITENESS = [HELLO, GOODBYE]

FRUITS = ["apples", "bananas"]
MEAT = ["pork", "chicken"]

FOODS = [FRUITS, MEAT]

random_Words = ["shoe", "bicycle", "school"]


#Here is the triple nested list.
VOCABULARY = [POLITENESS, FOODS, random_Words, "house"]

knowWord = False
userInput = input("say whatever")

#this checks for userInput in triple nested lists
#first it checks whether the first slot in vocabulary is nested,
#if that's the case it checks if the lists wiithin vocabulary[i] is nested and goes on like that.
#when finally it comes to a list that is not nested it checks for userInput in that list.

for i in range(len(VOCABULARY)):
    #if list item is not nested
    if any(isinstance(j, list) for j in VOCABULARY[i]) == False:
            #if userinput is inside a non-nested list
            if userInput not in VOCABULARY[i]:
                continue
            else:
                #if userInput is found
                knowWord = True
                break
    #if list is nested
    else:
        continue
    for k in range(len(VOCABULARY[i])):
        if any(isinstance(l, list) for l in VOCABULARY[i][k]) == False:
                if userInput not in VOCABULARY[i][k]:
                    continue
                else:
                    knowWord = True
                    break
        else:
            continue
        for m in range(len(VOCABULARY[i][k])):
            if any(isinstance(n, list) for n in VOCABULARY[i][k][m]) == False:
                    if userInput not in VOCABULARY[i][k][m]:
                        continue
                    else:
                        knowWord = True
                        break
            else:
                continue

if knowWord == True:
    print("YES")
else:
    print("I don't know that word")

To make your code work you can simply remove these lines: 要使代码正常工作,您可以删除以下几行:

#if list is nested
else:
    continue

To make the code nicer, you can use recursion. 为了使代码更好,可以使用递归。 This function finds out if a given word is inside however many nested lists you have: 此函数可以查找给定单词是否在内部,无论您拥有多少嵌套列表:

def findWord(word,l):
    words = []
    lists = []
    for entry in l:
        if isinstance(entry,list):
            lists.append(entry)
        else:
            words.append(entry)


    for w in words:
        if w == word:
            return True

    for ls in lists:
        if findWord(word,ls) == True:
            return True

    return False



if findWord(userInput,VOCABULARY) == True:
    print("YES")
else:
    print("I don't know that word")

If you want to use nested lists only then below might help you, otherwise, solution mentioned above to flatten the list or converting to Set can be used also 如果您只想使用嵌套列表,那么下面的内容可能会对您有所帮助,否则,也可以使用上面提到的将列表展平或转换为Set的解决方案

def check(val):
    for i in itertools.chain(VOCABULARY):
        if val in i:
            return True
    return False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM