题解 | 生词篇章查询

生词篇章查询

https://www.nowcoder.com/practice/3790042b79114ae5bd9f6eccdaeadfcd

import sys
from collections import defaultdict
#defaultdict(set) 可以在访问字典中不存在的值时自动初始化一个set

dict1 =defaultdict(set)

N = int(input())
for i in range(1, N+1):
    line = input().split()[1:]
    for v in line:
        dict1[v].add(i)

M = int(input())
for _ in range(M):
    wj = input()
    if wj not in dict1:
        print('\t')
    else:
        #将集合转为列表并排序,再使用map将每个元素转为str格式,通过join()拼接输出
        print(' '.join(map(str,sorted(list(dict1[wj])))))

全部评论

相关推荐

02-28 01:18
已编辑
南昌大学 后端工程师
黑皮白袜臭脚体育生:把开源经历放个人项目上边应该更好,就像大部分人都把实习经历放个人项目上边
点赞 评论 收藏
分享
04-08 21:39
已编辑
Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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