题解 | #数据分类处理#使用字典嵌套

数据分类处理

http://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd

```while True:
    try:
        lst = input().split()
        lst_str = lst[1:]
        num = input().split()
        num_str = num[1:]
        num_str = sorted(list(map(int, set(num_str))))
        res = {}
        for x in num_str:
            count = 0
            tmp_lst = []
            for i in range(len(lst_str)):
                if str(x) in lst_str[i]:
                    count += 1
                    tmp_lst.append(i)
                    tmp_lst.append(lst_str[i])
                else:
                    continue
            if count !=0:
                res[x] = {count: tmp_lst}
        out = []
        for x in res:
            out.append(x)
            for i in res[x]:
                out.append(i)
                for j in res[x][i]:
                    out.append(j)
        print(len(out),' '.join([str(x) for x in out]))         


    except:
        break
全部评论
基于大佬的代码,修改我自己能看懂的,哈哈 while True: try: lst = input().split() lst_str = lst[1:] num = input().split() num_str = num[1:] num_str = sorted(list(map(int, set(num_str)))) res = [] for x in num_str: count = 0 tmp_lst = [] out=[] for i in range(len(lst_str)): if str(x) in lst_str[i]: count += 1 tmp_lst.append(i) tmp_lst.append(lst_str[i]) else: continue if count !=0: out=[x]+[count]+tmp_lst res=res+out print(len(res),' '.join([str(x) for x in res])) except: break
点赞 回复 分享
发布于 2021-12-22 18:29

相关推荐

09-29 11:19
门头沟学院 Java
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务