2017-11-22 1 views
-1

나는 두 개의 텍스트 파일 dictionary.txt file_dictionary 및 output.txt file_output을 가지고 있습니다.이 파일들에는 dance, sanct 및 test의 세 가지 공통 단어가 있지만 두 단어를 비교할 때 반환되는 단어는 없습니다. 두 파일 :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

파이썬 문자열은 대소 문자를 구분합니다. 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 
관련 문제