题解 | #HJ25 数据分类处理#

数据分类处理

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

while True:
    try:
        i_seq = input().split()[1:]
        r_seq = sorted(list(set(input().split()[1:])), key=lambda x: int(x))
        result = []
        for r in r_seq:
            record = []
            for i_i in range(len(i_seq)):
                if (r in i_seq[i_i]):
                    record.append((i_i, i_seq[i_i]))
            if record:
                result.append((r, record))

        count = len(result) * 2 + sum([len(x[1]) * 2 for x in result])
        print(count, end=' ')
        for res in result:
            print(res[0], len(res[1]), end=' ')
            for item in res[1]:
                print(item[0], item[1], end=' ')
    except:
        break

Python 列表有 extend 方法,所以可以修改一下打印方式:

while True:
    try:
        i_seq = input().split()[1:]
        r_seq = sorted(list(set(input().split()[1:])), key=lambda x: int(x))
        result = []
        for r in r_seq:
            record = []
            for i_i in range(len(i_seq)):
                if (r in i_seq[i_i]):
                    record.extend([str(i_i), i_seq[i_i]])
            if record:
                result.extend([r, str(len(record)//2)])
                result.extend(record)

        count = len(result)
        print(count, ' '.join(result))
    except:
        break
全部评论

相关推荐

11-08 17:36
诺瓦科技_HR
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务