我需要制作一个函数,该函数获取文本主体并输出文本中所有唯一单词的列表.输出列表将包含字符串



这是我得到的。当我运行它时,它不起作用。有人能帮我指导我的代码出了什么问题吗?

def unique_words(text : str) ->list:
text = open(text, 'r')
text_contents = text.read()
text.close()
word_list = text_contents.split()
word = open(str, 'w')
unique = []
for word in word_list:
if word not in word_list:
file.append(str(word) + "n")

unique.sort((

返回(唯一(

这是我的代码

Set将保存所有唯一值。如果添加重复项,则集合将忽略它。

word_list = ['hello', 'hello', 'world', 'world']
unique = set()
for word in word_list:
if word not in unique:
unique.add(word)
print(unique)

使用set((返回唯一值并删除重复的

最新更新