Home > Mobile >  How to check if a word is in a list that is in another function?
How to check if a word is in a list that is in another function?

Time:11-10

To put it simply, I was given a .txt file with a list of words in it, then asked to read it in python and store it in a list with a function. So far, no problem. I wrote something that looks like this:

def load_lexicon():

    with open("lexicon.txt", "r") as f:
        content_list = f.read().splitlines()
    
    lexique = open("lexicon.txt", "r")

    liste_des_mots = []
    
    for i in lexique:
        ligne = i.strip()
        liste = ligne.split()
        liste_des_mots.append(liste)
     
    return liste_des_mots

The problem is that I would like to create a function that accesses this list that I created in the previous function and checks if a given word is there or not. I tried something like this:

def check_word():
    search_word = input("enter the word you want to search for: ")
    if search_word in load_lexicon():
        print ('word found')
    else:
        print ('word not found')

But the thing is, no matter what word I put in, whether it's in the .txt file or not it always returns that the word is not in it, for example:

enter the word you want to search for: 'a'
word not found

or

enter the word you want to search for: a
word not found

just so you know, a is in the .txt file

CodePudding user response:

First you open the file and read the contents as content_list then you make another file handle called lexique. So you never actually do anything with the file contents. Additionally as @quamrana mentions you would need to either iterate through each word on each line or use extends to add them to your list otherwise that list is a list of lines which is ironically exactly what content_list was before that for loop.

def load_lexicon():
    with open("lexicon.txt", "r") as f:
        content_list = f.readlines()

    liste_des_mots = []

    for i in content_list:
        ligne = i.strip()
        liste = ligne.split()
        liste_des_mots.extend(liste)

    return liste_des_mots

CodePudding user response:

def load_lexicon():

    with open("lexicon.txt", "r") as f:
        content_list = f.read().splitlines()
    global liste_des_mots
    liste_des_mots = []

    lexique = open("lexicon.txt", "r")

    for i in lexique:     
        ligne = i.strip()
        #print(ligne)
        liste = ligne.split()    
        liste_des_mots.append(liste)    
    return liste_des_mots

#print(liste_des_mots)    
count =len(liste_des_mots)


def check_word():
    search_word = input("enter the word you want to search for: ")
    for i in range(count):
        #print(liste_des_mots[i])
        if search_word in liste_des_mots[i]:      
            print('word found')
            break
    else:   
        print('word not found')
                
check_word()

in your code you must focus on this line : if search_word in load_lexicon(): This is most likely a nested list. For this reason, it cannot make the call correctly. Once again a iteration is required within the list.

I tried to fix it. I hope it helps

  • Related