这是一个优化版本,它将逐行读取文件而不将其全部加载到内存中:seen_words = set()with open('text.txt', 'r') as my_file: for line in my_file: for word in line.lower().split(): if word not in seen_words: print(word) seen_words.add(word)