Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 打印两个文本文件中常见的单词_Python 3.x_Python 2.7 - Fatal编程技术网

Python 3.x 打印两个文本文件中常见的单词

Python 3.x 打印两个文本文件中常见的单词,python-3.x,python-2.7,Python 3.x,Python 2.7,我有两个名为dictionary.txt和output.txt的文本文件。这两个文件都有三个常用单词dance、sanct和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.stri

我有两个名为dictionary.txt和output.txt的文本文件。这两个文件都有三个常用单词dance、sanct和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