私信  •  关注

Georg Schölly

Georg Schölly 最近创建的主题
Georg Schölly 最近回复了
17 年前
回复了 Georg Schölly 创建的主题 » 使用python检查单词

你的 refDict 可能是错的。这个 in 关键字检查值是否在字典的关键字中。我相信你把你的话当作价值观。

我建议使用 set 而不是字典。

knownwords = set("dog", "cat")
knownwords.add("apple")

text = "The dog eats an apple."
for word in text.split(" "):
    # to ignore case word is converted to lowercase
    if word.lower() not in knownwords:
        print word
# The
# eats
# an
# apple.       <- doesn't work because of the dot