DefaultDict Tutorial

Sort by

recency

|

1075 Discussions

|

  • + 0 comments
    lengths = input().split()
    target_lst  = []
    seek_lst = []
    for n in range(int(lengths[0])):
        target_lst.append(input())
    for n in range(int(lengths[1])):
        seek_lst.append(input())
     
    for n in seek_lst:
        target_index = []
        if n in target_lst:
            for c,x in enumerate(target_lst):
                if x==n:
                    target_index.append(str(c+1))
            print(' '.join(target_index))
        else:
            print("-1")
    
  • + 0 comments
    from collections import defaultdict
    d = defaultdict(list)
    
    line1 = list(map(int, input().split()))
    
    n = line1[0]
    m = line1[1]
    
    for i in range(n):
        d['A'].append(input())
        
    for j in range(m):
        d['B'].append(input())
        
    
    for p in list(d['B']):
        if p in list(d['A']):
            output_list = []
            for q in range(len(list(d['A']))):
                if p == list(d['A'])[q]:
                    output_list.append(str(q+1))
            print(" ".join(output_list))
        else:
            print("-1")
    
  • + 0 comments
    1. from collections import defaultdict
    2. n,m = map(int,input().split())
    3. d = defaultdict(list)
    4. for i in range(n):
    5. z = input()
    6. d[z].append(i+1)
    7. for j in range(m):
    8. j = input()
    9. if j in d:
    10. print(*d[j])
    11. else:
    12. print(-1)
  • + 0 comments
    from collections import defaultdict
    n,m = map(int,input().split())
    d = defaultdict(list)
    for i in range(n):
        A = input()
        d[A].append(i+1)
    
    for j in range(m):
        B = input()
        if B in d.keys():
            val = d[B]
            print(*val)
        else:
            print(-1)
    
  • + 0 comments

    from collections import defaultdict n,m = list(map(int, input().split()))

    A = defaultdict(list) for i in range(1, n+1): A[input()].append(i)

    for _ in range(m): print(*A[input()] or [-1])