题解 | #简单错误记录#

简单错误记录

https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

ls = []   # 储存键
dic = {}  # 储存键-值对
while True:
    try:
        msg = input().split()   
        msg[0] = msg[0].split('\\')[-1]        # 路径\分割,只取最后一个
        msg = ' '.join([msg[0][-16:], msg[1]]) # 取后16位及行号(str[-16],num)-> (str[-16] num)  此时属性为字符串
        if msg not in dic.keys():              # 将msg记为字典的key值并判断是否存在
            ls.append(msg)                     # 不存在就将其计入列表ls
            dic[msg] = 1                          # 将msg为key的value记录为1
        else:
            dic[msg] += 1                         # 存在msg就在字典中对应值增加计数
    except:
        break
for item in ls[-8:]:                           # 正序遍历后八个存储的键
    print(item, dic[item])                     # 打印键-值对

摘抄作业,这题我写了好久结果还是不对,最后一个用例通不过,实在心累

listtemp1 = []
listtemp2 = []
listtemp3 = []
codelisttemp = []
countlisttemp = []

while True:
    try:
        testinput = input()
        inputsplitlist1 = testinput.split('\\')
        inputsplitlist2 = inputsplitlist1[-1].split(' ')
        coderows = int(inputsplitlist2[-1])
        codelisttemp.append(coderows)
        listtemp1.append(inputsplitlist2[-2])
    except:
        break

temp1length = len(listtemp1)
for i in range(temp1length):
    if len(listtemp1[i]) > 16:
        listtemp1[i] = listtemp1[i][-16:]

for j in range(temp1length):
    listtemp2.append([listtemp1[j],codelisttemp[j]])

for k in range(temp1length):
    if listtemp2[k] not in listtemp3:
        listtemp3.append(listtemp2[k])
        countlisttemp.append(1)
    else:
        index = listtemp2.index(listtemp2[k])
        countlisttemp[index] += 1

for k in range(8):
    try:
        if len(listtemp3) > 8:
            temp3length = len(listtemp3)
            startindex = k + (temp3length - 8)
            print(listtemp3[startindex][0], listtemp3[startindex][1], countlisttemp[startindex])
        else:
            print(listtemp3[k][0], listtemp3[k][1], countlisttemp[k])
    except:
        continue
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:25
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务