我只想将两个文本中的单词放在一个集合中,以获得所有独特的单词。当我运行此代码时,我得到一个类型错误:不可用类型:'list'
我可以采取哪些不同的方式?
infile1 = open("random.txt", 'r')
infile2 = open("random_encr.txt", 'r')
data1 = infile1.read()
data2 = infile2.read()
#close files
infile1.close()
infile2.close()
# replace all dots with empty string
data1 = data1.replace('.', '')
data2 = data2.replace('.', '')
# split words in data (splitted by whitespace) and save in words
words1 = data1.split()
words2 = data2.split()
set1 = set([words1])
set2 = set([words2])
set1.update(set2)
print(set1)
答案 0 :(得分:4)
问题是您传递给set
的参数出错:
set1 = set([words1])
定义了一个项目的列表 - 包含单词列表。
[["a", "b", "c"]]
... ["a", "b", "c"]
时。删除[]
并执行set1 = set(words1)
答案 1 :(得分:0)
这是生成任一文档中出现的所有单词列表的一种方法:
infile1 = open("1.txt", 'r')
infile2 = open("2.txt", 'r')
data1 = infile1.read()
data2 = infile2.read()
#close files
infile1.close()
infile2.close()
# replace all dots with empty string
data1 = data1.replace('.', '')
data2 = data2.replace('.', '')
# split words in data (splitted by whitespace) and save in words
words1 = data1.split()
words2 = data2.split()
#create combined list
combinedList = []
for word in words1:
if word not in combinedList:
combinedList.append(word)
for word in words2:
if word not in combinedList:
combinedList.append(word)
print(combinedList)