目前我能够在句子列表中打印出匹配的字典项。我宁愿打印出句子列表的元素。请建议最好的方法来实现这一目标。以下是我的代码。谢谢。
sentences = "The book was awesome and envious","splendid job done by those guys", "that was an amazing sale"
dictionary = "awesome","amazing", "fantastic","envious"
##Find Matches
for match in dictionary:
if any(match in value for value in sentences):
print match
答案 0 :(得分:1)
all_words = re.sub("[^a-zA-Z]","",main_text).split()
dictionary_words = open("dictionary.txt").read().split()
print "words found:",set(all_words).intersection(dictionary_words)
print "words not found:",set(all_words).difference(dictionary_words)
是你在寻找什么?
如果您有一个句子列表,并且您想要创建一个包含字典单词的句子的新列表,您可以执行类似
的操作sentances_with_words_in_dict = [sentence for sentence in all_sentences if set(sentence.split()).intersection(dictionary_words)]