2017-11-22 27 views
-1

dictionary.txt file_dictionaryとoutput.txt file_outputという2つのテキストファイルがあります。これらのファイルにはdance、sanct、testの3つの一般的な単語がありますが、 2つのファイル:2つのテキストファイルに共通の単語を印刷する

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) 

私は間違ったことを得ることができません。助けてください!

答えて

0

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 
関連する問題