在两个文本文件中打印单词



我有两个文本文件,称为dictionary.txtfile_dictionary and output.txtfile_output.thy theus theus theus theus theus dance,sanct and test test and test,但我在比较两个方面返回了任何单词文件:

with open('output.txt') as words_file:
with open('dictionary.txt') as dict_file:
    all_strings = set(map(str.strip, dict_file))
    words = set(map(str.strip, words_file))
    for word in all_strings.intersection(words):
        print(word) 

我无法得到问题。请帮助!

python字符串对案例敏感。output.txt中的字符串是上案例,因此您需要在进行比较之前将它们转换为低案例:

# remove set from this line
words = map(str.strip, words_file)   
# convert list to lower-case, then apply set operation
words = set(map(str.lower, words))
# everything else same as before
for word in all_strings.intersection(words):
       ...

输出:

dance
test
sanct

最新更新