DefaultDict Tutorial

Sort by

recency

|

1126 Discussions

|

  • + 0 comments

    if u want u can create a defaultdict which creates an empty list for all the values it get's from group a and append the indeces of those values or u create ur own dictionary append the indeces of the list a and check with list b and print the indeces of each element.

    d={}
    for i in range(1,n+1):
        w=input()
        if w not in d:
            d[w]=[]
        d[w].append(str(i))
    
    for _ in range(m):
        y=input()
        if y in d:
            print(' '.join(d[y]))
        else:
            print(-1)
    
  • + 0 comments
    from collections import defaultdict
    
    n, m = map(int, input().split())
    group_a = defaultdict(list)
    
    for i in range(n):
        group_a[input()].append(i+1)
    
    for i in range(m):
        word = input()
        if word in group_a.keys():
            print(*group_a[word], ' ')
        else:
            print('-1')
    
  • + 0 comments

    from collections import defaultdict

    n, m = map(int, input().split())

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

    groupB = [input().strip() for _ in range(m)]

    for word in groupB: if word in groupA: print(*groupA[word]) else: print(-1)

  • + 0 comments

    from collections import defaultdict

    if name == 'main':
    N = list(map(int, input().split())) d = defaultdict(list) for i in range(N[0]): letter = input() d[letter].append(int(i+1)) #print(d)

    for i in range(N[1]):
        letter = input()
        if letter in d.keys():
            print(*d[letter])
        else:
            print(-1)
    

    purchase_order(arr, sizes)

  • + 0 comments

    This code passes half the test cases but fails the other half. Any ideas on why?

    # # Enter your code here. Read input from STDIN. Print output to STDOUT
    from collections import defaultdict
    A, B = input().split()
    listA = []
    listB = []
    for i in range(int(A)):
        item = input()
        listA.append(item)
    for i in range(int(B)):
        item = input()
        listB.append(item)
    b_in_a = defaultdict(list)
    for i in range(len(listB)):
        if listB[i] not in listA:
            b_in_a[listB[i]] = -1
        else:
            for j in range(len(listA)):
                if listB[i] == listA[j]:
                    b_in_a[listA[j]].append(j+1)
    for key, value in b_in_a.items():
        print(*value)  # The * operator unpacks the list and separates elements by spaces