0

1 つ以上の単語がリストに含まれているかどうかを確認する関数を定義しましたが、正常に動作しますが、コードをどのように変更する必要があるかを理解しようとしています。単語がリストにあります。これらは、私がいじってきた2つの別々の機能です:

これはブール値のないもので、単語とテキストに表示されるかどうかを完全に出力しますが、関数はブール値を出力しません (出力するだけで、少し面倒です)

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")

これはブール値を出力しますが、1 つの単語のみです。出力としてブール値のリストを取得し、印刷しないようにそれらを組み合わせる方法を考えています

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)

私はこれで終わりました、それはかなりうまくいきます(私の変数/関数名は実際にはプロジェクトでフランス語です。これはフランスの大学での宿題のためです)

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)

唯一厄介なことは、ブール値が英語で表示されることです。

4

2 に答える 2

0

入力の取得にバグがあります。入力の代わりに raw_input を使用します。

def isword(file):
    wordlist= raw_input("Which word(s) would you like to check for in the text? ").split()
    file=chopup(file)
    return [word.lower() in file for word in wordlist]

参考までに、必要はありませんn+=1。for ループは自動的に n をインクリメントします。

于 2012-06-15T09:40:59.707 に答える
0

みてみましょう:

  • ブール値を返すようになりましたが、それはリストである必要があります。したがって、関数の最初と最後result = []にどこかに置く必要があります。return result
  • あとは、考えている単語ごとにTrueorをそのリストに追加するだけです。False単語がファイルに含まれているかどうかは既に計算されているため、これはそれほど難しくありません。
于 2012-06-15T09:41:17.087 に答える