处理:
1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并)
2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并)
3.输入的文件可能带路径,记录文件名称不能带路径
数据范围:输入错误记录数量满足 ,每条记录的长度满足
一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。
文件路径为windows格式
如:E:\V1R2\product\fpgadrive.c 1325
将所有的记录统计并将结果输出,格式:文件名代码行数数目,一个空格隔开,如: fpgadrive.c 1325 1
结果根据数目从多到少排序,数目相同的情况下,按照输入第一次出现顺序排序。
如果超过8条记录,则只输出前8条记录.
如果文件名的长度超过16个字符,则只输出后16个字符
E:\V1R2\product\fpgadrive.c 1325
fpgadrive.c 1325 1
import collections d = collections.defaultdict(int) while True: try: d[input().split('\\')[-1]] += 1 except: break p = sorted(list(d.items()), key=lambda k: -k[1]) for i in range(min(len(p), 8)): print(p[i][0].split()[0][-16:], p[i][0].split()[1], p[i][1])
import sys res = {} for line in sys.stdin.readlines(): path, line_number = [x for x in line.split()] file_name = path.split("\\")[-1] key = file_name + line_number if key in res: res[key][2] += 1 else: temp = [] if len(file_name) > 16: temp.append(str(file_name[-16:])) else: temp.append(file_name) temp.append(line_number) temp.append(1) res[key] = temp out = [] for v in res.values(): out.append(v) out.sort(key=lambda ele: ele[2], reverse=True) n = len(out) if len(out) <= 8 else 8 for i in range(n): print(out[i][0], out[i][1], out[i][2])
啥也不会,就会傻瓜式的方法,先存储错误信息,以及同类错误信息的次数,这两个列表索引值对应。然后对错误信息的次数进行排序,并返回排序前的索引值,最后根据该索引值,从大到小索引对应的错误信息并打印,超过8个不打印。
errorMsg = [] errorNum = [0] * 99999 # 由于错误信息数目未知,因此初始化一个比较大的列表每种存错误信息的数量 while True: try: error = input().strip().split('\\')[-1] # 注意strip去掉每行末尾的空格或换行符 if error not in errorMsg: errorMsg.append(error) errorNum[errorMsg.index(error)] = 1 else: errorNum[errorMsg.index(error)] += 1 except: break errorNum = errorNum[:len(errorMsg)] # 跟据错误信息的种类,将errorNum列表末尾无用的0值去掉,便于排序 # print(errorNum, errorMsg) # 该方法返回排序前(从大到小)的索引,以及排序好的列表 idx, _ = list(zip(*sorted(enumerate(errorNum), key=lambda x:x[1], reverse=True))) msgCnt = 0 # 累计打印次数,最多打印8条信息 for i in idx: msgCnt += 1 if len(errorMsg[i].split()[0]) > 16: msg = errorMsg[i].split()[0][-16:] + ' ' + errorMsg[i].split()[1] + ' ' + str(errorNum[i]) else: msg = errorMsg[i] + ' ' + str(errorNum[i]) if msgCnt > 8: break print(msg)
team = [] while True: line = stdin.readline().strip() if line == '': break item = line.split(' ') item = [i for i in item] team.append(item) # F=codecs.open('F://新建文本文档.txt','r',encoding='utf-8') # for v in F.readlines(): # team.append(v.strip().split(' ')) # print(team) array=[os.path.basename(v[0])+"_"+v[-1] for v in team] count={} for v in array: if v in count: count[v]+=1 else: count[v]=1 lstTuple = sorted(count.items(), key=lambda d: d[1], reverse=True) ct=0 for key in lstTuple: if ct>7: break ct+=1 f, r = str(key[0]).split('_') if f.index('.') >= 16: print(f[f.index('.')-16:],r,key[-1]) else: print(f, r, key[-1]) if __name__ == '__main__': result=Solution() result.error_log()
error_reconds = [] while True: try: route, row_num = [x for x in raw_input().split()] row_num = int(row_num) recond_tmp = [] name = route.split("\\")[-1] name1 = name if len(name) < 16 else name[-16:] count = 1 recond_tmp.append(name1) recond_tmp.append(row_num) recond_tmp.append(count) if len(name) > 16: recond_tmp.append(name) same_flag = False for recond in error_reconds: if recond[0] == recond_tmp[0] and recond[1] == recond_tmp[1]: if len(recond_tmp) == len(recond) == 4 and recond_tmp[3] != recond[3]: same_flag = False else: same_flag = True recond[2] += 1 if not same_flag: error_reconds.append(recond_tmp) except: error_reconds = sorted(error_reconds, key=lambda x:x[2], reverse=True) error_reconds = error_reconds if len(error_reconds) < 8 else error_reconds[:8] for recond in error_reconds: print recond[0], recond[1], recond[2] break
sorted(res.items(), key=lambda x: x[1], reverse=True) #可以对字典res的values进行排序 (10, 3) > (10, 2) > (9, 5) (2956)# tuple的大小比较完整代码:
res = {} i = 0 while True: try: s = input() file_error = s.split('\\')[-1] found = False for key in res.keys(): if file_error in key: res[key] = (res[key][0]+1, res[key][1]) # 数量+1 found = True if not found: file_error_key = '{}'.format(file_error) res[file_error_key] = (1, 9999-i) (2957)# 把数量和出现次数放入键值 i += 1 except: break sort_res = sorted(res.items(), key=lambda x: x[1], reverse=True) for i in range(min(8, len(sort_res))): name, num = sort_res[i] name = name.split(' ') file_name, error = ''.join(name[:-1]), name[-1] file_name = file_name[-16:] print('{} {} {}'.format(file_name, error, num[0]))
import collections import sys def fun(): record = collections.OrderedDict() # record={} while True: try: ## input_line=input().strip().split() input_line=sys.stdin.readline().strip().split() file_name=input_line[0].split('\\')[-1] key=str(file_name+(' ')+input_line[1]) # print("key:",key) ## key=input().strip().split('\\')[-1] # key=sys.stdin.readline().strip().split('\\')[-1] # print("key:",key) # input_line=input().strip() # index =input_line.rfind('\\') # key=input_line[index+1:] # print("key:",key) if(key not in record): # record.update({key:1}) record[key]=1 else: record[key]+=1 except: # print("error") break record_out = sorted(record.items(), key=lambda x: x[1], reverse=True) for k, v in record_out[:min(len(record_out),8)]: if (len(k) > 16): print(k[-16:], v) elif (len(k)<=16 and len(k)>0): print(k, v) if __name__ == "__main__": fun()
# 目的是构建一个 wrong_dict = { ((filename, line): [number, rank]) } wrong_dict = {} # 用字典要更好,可以轻松地计数 rank = 1 # 记录顺序的一个int型的数据 # 持续获得输入,若无数据输入,则先输出然后再退出循环 while 1: try: filename, line = (input().split('\\')[-1]).split() # 先以反斜杠划分,再以空格划分 if (filename, line) in wrong_dict: # 有就让这个键的值中的记录出现次数的值+1 wrong_dict[(filename, line)][0] += 1 else: # 没有就新建一个键值对,并且需要注意让记录顺序的数据加1 wrong_dict[(filename, line)] = [1, rank] rank += 1 except: # 先排number,number降序;(number相同的情况下)再排rank,rank增序 # 并且只取8个数据或者不到8个数据 result_list = sorted(wrong_dict.items(), key = lambda x: (-x[1][0], x[1][1]))[: 8] # 最后按照要求打印: for ((filename, line), [number, rank]) in result_list: print(filename[-16: ], line, number) # 退出循环 break
import collections
d = collections.OrderedDict()
while True:
try:
ele = input().split('\\')[-1].strip('\n')
if not ele:
break
if ele in d:
d[ele] += 1
else:
d[ele] = 1
except:
break
d = sorted(d.items(), key=lambda k: k[1], reverse=True)
for i in range(min(len(d), 8)):
t = d[i][0].split(' ')
print(t[0][-16:], t[1], d[i][1])
import sys record,d=[],[] for line in sys.stdin:#循环读取输入 error=line.split('\\')[-1].strip()#获取最后一个\右边的字符 if error not in d:#该文件名和代码行数未出现过,则添加进记录,并计数为1 d.append(error) record.append([error,1]) else:#已出现过,则计数+1 index=d.index(error) record[index][1]+=1 record=sorted(record,key=lambda x:x[1],reverse=True)#按次数倒序排序 if len(record)>8:#只取前8条记录 record=record[:8] for x in record: l=len(x[0].split()[0]) if l>16:#文件名超过16个字符,则取后16个字符 x[0]=x[0][l-16:] print(x[0]+' '+str(x[1]))
import sys input_records = [] for line in sys.stdin: input_records.append(line.strip('\n')) records = {} for i in range(len(input_records)): error = input_records[i].split('\\')[-1] if not error in records.keys(): records[error] = [1,-i] elif error in records.keys(): records[error][0] += 1 else: continue #print(records) sorted_record = sorted(records.items(),key=lambda item:item[1],reverse=True) #print(out) out = '' tag = 0 #print(sorted_record) for r in sorted_record: if tag >= 8: break file_name = r[0].split(' ')[0] line_num = r[0].split(' ')[1] if len(file_name) > 16: file_name = file_name[-16:] #r[0] = file_name + ' ' + line_nume re = file_name + ' ' + line_num + ' ' + str(r[1][0]) print(re) #out += re tag += 1 #print(out)
#coding=utf-8 import sys d={} while True: f=sys.stdin.readline().strip() if not f: break ff=f.split('\\')[-1] if ff not in d.keys(): d[ff]=1 else: d[ff]+=1 for i in range(min(len(d.values()),8)): m=max(d.values()) for j in d: if d[j]==m: if len(j.split()[0])>16: j1=j.split() print '{0} {1} {2}'.format(j1[0][-16:],j1[1],m) else: print '{0} {1}'.format(j,m) d[j]=0 break 请大佬看看为什么错了
from collections import OrderedDict def solution(lines): records = OrderedDict() # 有序字典,便于后期稳定输出 for e in lines: line = e.split() name = line[0].split('\\')[-1] if (name, line[1]) not in records:records[(name, line[1])] = 1 else:records[(name, line[1])] += 1 return records import sys try: while True: lines = sys.stdin.readlines() # print lines if not lines: break ret = solution(lines) # print ret #result = zip(ret.keys(), ret.values()) result = sorted(ret.iteritems(),key=lambda x:x[1], reverse=True) # 本身按递增排序的结果就是不稳定的,reverse之后就满足了。。 # print result # print result[:8] for e in result[:8]: if len(e[0][0]) > 16:print e[0][0][-16:],e[0][1],e[1] else: print e[0][0],e[0][1],e[1] except: # print result pass