随手记
返回目录
字典去重 2026-11-27 python文件: 解析同目录下的1.txt,去重,将结果保存进入第二个文件1out.txt 这个1.txt文件中全是中文的单词,一行是一个单词。 -------------------------------------------------------------------- def dedup_word_file(): seen = set() result = [] with open("1.txt", "r", encoding="utf-8") as f: for line in f: word = line.strip() # 跳过空行 if not word: continue if word not in seen: seen.add(word) result.append(word) with open("1out.txt", "w", encoding="utf-8") as f_out: for w in result: f_out.write(w + "\n") print(f"处理完成!一共 {len(result)} 个不重复词语,已保存到 1out.txt") if __name__ == "__main__": dedup_word_file()