Python 为什么我得到一个不可修复的类型错误

Python 为什么我得到一个不可修复的类型错误,python,input,split,store,words,Python,Input,Split,Store,Words,我试图从两个输入文件中找到所有匹配的单词,但我一直得到“TypeError:unhabable type:'list'”。我不知道为什么。有人能告诉我我做错了什么以及如何修复它吗 #!/usr/bin/python3 #First file file = raw_input("Please enter the name of the first file: ") store = open(file) new = store.read() #Second file file2 = raw_

我试图从两个输入文件中找到所有匹配的单词,但我一直得到“TypeError:unhabable type:'list'”。我不知道为什么。有人能告诉我我做错了什么以及如何修复它吗

#!/usr/bin/python3

#First file
file = raw_input("Please enter the name of the first file: ")

store = open(file)

new = store.read()

#Second file
file2 = raw_input("Please enter the name of the second file: ")

store2 = open(file2)

new = store2.read()

words = set(line.strip() for line in new)

for line in new:
    word2 = line.split()
    if word2 in words:
            print words

您将获得
TypeError:unhable类型:“list”
异常,因为
word2=line.split()
正在返回一个list对象。 您正试图在
单词中搜索列表(不可损坏的对象)

例如:

>>> word2 = 'abc'
>>>
>>> word2.split() in set(['abc', 'def'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

当我这样做的时候,我会得到所有匹配的字母。我需要所有匹配的单词扫描你添加一个你所面对的示例案例?我得到一堆像这样的行“set([“,”,“,”,“,”,“,?”,“a”,“I”,“I”,“M”,“W”,“a”,“c”,“b”,“e”,“d”,“g”,“f”,“I”,“h”,“k”,“M”,“l”,“o”,“n”,“p”,“s”,“r”,“u”,“t”,“W”,“v”,“y”),我需要它返回所有匹配的单词,如“apple”“等等,这是因为
words=set(line.strip()表示新行)
。在这里,您的
new
对象将返回整个文件内容,当您迭代它时,它将迭代每个字符。您应该逐行迭代它,并按空间分割它们,然后将其附加到某个列表中。最后调用该列表上的
set
函数。
>>> word2.strip() in set(['abc', 'def'])
True