Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck for presence of multiple words in list and return list of boolean
    primarykey
    data
    text
    <p>I've defined a function that checks if one or more words are in a list, and it works fine, but now I'm trying to figure out how I should change my code so I get a <strong>list</strong> of boolean values depending on whether the word is in the list. These are the two separate functions I've been fiddling with:</p> <p><strong>This is the one without booleans, and it works perfectly to print the words and whether or not they appear in the text, but the function doesn't output a boolean value (it just prints, which is a bit messy I know)</strong></p> <pre><code>def isword(file): wordlist=input("Which word(s) would you like to check for in the text? ") wordlist=wordlist() file=chopup(file) ##This is another function of mine that splits a string(file) into a list for n in range(0,len(wordlist)): word=wordlist[n] n+=1 word=word.lower() ##so case doesn't matter when the person enters the word(s) if word in file: print(word, ":TRUE") for i in range(0,len(file)): if file[i]==word: print(i) else: print(word," :FALSE") </code></pre> <p><strong>This one outputs a boolean but for one word only. I'm wondering how to combine them so that I get a list of booleans as the output, no printing</strong></p> <pre><code>def isword(file): a=True wordlist=input("Which word(s) would you like to check for in the text? ") wordlist=wordlist() file=chopup(file) ##This is another function of mine that splits a string(file) into a list for n in range(0,len(wordlist)): word=wordlist[n] n+=1 word=word.lower() if word in file: a=a else: a=False return(a) </code></pre> <p><strong>I ended up with this, it works pretty well (my variable/function names are actually in French in the project cause this is for homework at a French uni)</strong></p> <pre><code>def presmot(fichier): result=[] listemots=input("Entrez le/les mots dont vous voulez vérifier la présence : ") listemots=listemots.split() fichier=decoupage(fichier) for n in range(0,len(listemots)): mot=listemots[n] mot=mot.lower() def testemot(mot): a=True if mot in fichier: a=a else: a=False return(a) result=[(mot,testemot(mot)) for mot in listemots] return(result) </code></pre> <p>The only annoying thing is that the Boolean comes up in English, oh well!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload