#!/usr/bin/python # -*- coding: UTF-8 -*- import sys ''' 在第一个文件的基础上,添加后续的全部不重复的2字词语。 ''' _list_unique = set() _list_temp = set() base_txt_file = sys.argv[1] new_added_txt_file =sys.argv[2] base_txt_file_with_added = sys.argv[3] with open(base_txt_file, 'r', encoding='utf-8-sig') as f: for word in f.read().splitlines(): _list_unique.add(word[0]) _list_unique.add(word[1]) _list_unique.add(word[2]) _list_unique.add(word[3]) print(len(_list_unique)) with open(base_txt_file_with_added, 'w', encoding='utf-8') as final_f: with open(new_added_txt_file, 'r', encoding='utf-8-sig') as f: for word in f.read().splitlines(): #print(word) if len(word) == 2: _list_temp = _list_unique.copy() _list_temp.add(word[0]) _list_temp.add(word[1]) if len(_list_temp) - len(_list_unique) == 2: _list_unique.add(word[0]) _list_unique.add(word[1]) final_f.write(word) final_f.write('\n') else: print(word) print(len(word))