题解 | #HJ19 简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
import sys error_dic = {} error_list = [] for line in sys.stdin: file_path, row_num = line.split() file_name = file_path.split("\\")[-1][-16:] error_key = file_name + " " + row_num if error_key in error_dic: error_dic[error_key] += 1 else: error_dic[error_key] = 1 error_list.append(error_key) for error_key in error_list[-8:]: print(error_key, error_dic[error_key]) # 说明: # 使用字符串作为error_key,保证唯一性。 # 在error_key不在error_dic的情况下,把它添加到error_list,保证元素不重复,也满足了第一次出现。 # 方法二:使用元组作为error_key # import sys # input_list = [] # error_dic = {} # for line in sys.stdin: # file_path, row_num = line.split() # file_name = file_path.split("\\")[-1][-16:] # error_key = (file_name, row_num) # if error_key not in error_dic: # error_dic[error_key] = 1 # else: # error_dic[error_key] += 1 # output = [] # for k, v in error_dic.items(): # output.append([k[0], k[1], v]) # for li in output[-8:]: # print(li[0], li[1], li[2])