Python 3.x 打印两个文本文件中常见的单词
我有两个名为dictionary.txt和output.txt的文本文件。这两个文件都有三个常用单词dance、sanct和test,但在比较这两个文件时,我没有得到任何返回的单词: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
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