#!/usr/bin/python # -*- coding: UTF-8 -*- import sys ''' 在第一个文件的基础上,添加后续的全部不重复的四字成语。 再加一个限定条件,即新加的四个字全部都属于一级字表。 ''' _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] level1_words_file = '../dict/universal_char_list_1_3500.txt' level1_words = '' with open(level1_words_file, 'r', encoding='utf-8-sig') as f: level1_words = f.read().splitlines() 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]) def if_all_level_one(word): for char in word: try: _index = level1_words[0].index(char) except: return False return True 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) == 4: _list_temp = _list_unique.copy() _list_temp.add(word[0]) _list_temp.add(word[1]) _list_temp.add(word[2]) _list_temp.add(word[3]) if len(_list_temp) - len(_list_unique) == 4: if if_all_level_one(word): _list_unique.add(word[0]) _list_unique.add(word[1]) _list_unique.add(word[2]) _list_unique.add(word[3]) final_f.write(word) final_f.write('\n') else: print(word) print(len(word))