Python如何检查单词是否在列表和输入中?

Python如何检查单词是否在列表和输入中?,python,python-3.x,words,Python,Python 3.x,Words,我试图让我的程序通过一个输入句子(例如“你好!”) 并查看输入中是否有任何单词在列表中。 以下是迄今为止的代码: def findWholeWord(w): return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search i.upper() #i is the inputted variable as a string WordsDocument = open('WordsDocument.txt').readl

我试图让我的程序通过一个输入句子(例如“你好!”) 并查看输入中是否有任何单词在列表中。 以下是迄今为止的代码:

def findWholeWord(w):
    return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
i.upper() #i is the inputted variable as a string
WordsDocument = open('WordsDocument.txt').readlines()
for words in WordsDocument:
    WordsList.append(words)
for word in i:
    if findWholeWord(word) in WordsList:
        print("Word Match")
有人能帮我制定一个更好的解决方案/修复此问题,使其正常工作吗

import re

def findWholeWord(w):               # input string w

    match_list = []                 # list containing matched words
    input_list = w.split(" ")

    file = open('WordsDocument.txt', 'r')
    text = file.read().lower()
    file.close()
    text = re.sub('[^a-z\ \']+', " ", text)
    words_list = list(text.split()) 

    for word in input_list:
        if word in words_list:
            print("Word Found: " + str(word))
            match_list.append(word)
    return match_list