您可以使用以下Python代码来实现您的需求: ```python def sort_words_by_freq(code_table): word_dict = {} # 将数据解析成字典 for line in code_table: code, freq, word = line.strip().split('=') if code not in word_dict: word_dict[code] = [] word_dict[code].append((int(freq), word)) sorted_lines = [] # 对每个编码的单词按词频进行排序 for code, words in word_dict.items(): sorted_words = sorted(words) sorted_line = f'{code}=' + '\t'.join([word for freq, word in sorted_words]) sorted_lines.append(sorted_line) # 返回按需求排序的所有行 return sorted_lines def write_sorted_lines(sorted_lines, output_file): with open(output_file, 'w') as file: for line in sorted_lines: file.write(line + '\n') # 读取码表文件,每一行表示一个编码和单词 with open('code_table.txt', 'r') as file: code_table = file.readlines() # 对码表进行排序 sorted_lines = sort_words_by_freq(code_table) # 将排序后的行写入新文件中 write_sorted_lines(sorted_lines, 'sorted_code_table.txt') ``` 请将您的码表文件重命名为`code_table.txt`,运行上述代码后,将生成名为`sorted_code_table.txt`的新文件,其中的内容满足您的要求。 [2024-06-01 15:59:25 | AI写代码神器 | 828点数解答]