DefaultDict Tutorial

Sort by

recency

|

1132 Discussions

|

  • + 0 comments

    I've left my solution below in case it helps anyone. I'm not sure if its the best solution, but it worked!

    from collections import defaultdict

    if name == 'main': n, m = list(map(int, input().split())) d = defaultdict(list)

    for _ in range(n):
        d['a'].append(input())
    
    
    for _ in range(m):
        current = input().strip()
        indices = []
        for i in range(n):
            if current == d['a'][i]:
                index = i + 1
                indices.append(index)     
        if indices:
            print(' '.join(map(str, indices)))
        else:
            print('-1')
    
  • + 1 comment

    Check this: from collections import defaultdict d = defaultdict(list)

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

    for i in range(1,n+1): d[input()].append(i)

    for i in range(m): word_B = input() if word_B in d: print(*d[word_B]) else: print(-1)

  • + 0 comments

    I defaulted to not using default dict

    if __name__ == "__main__":
        n,m = map(int, input().split(" "))
        a, b = [], []
        for _ in range(n): a.append(input())
        for _ in range(m): b.append(input())
        
        for b_word in b:
            indices = [i+1 for i,word in enumerate(a) if word == b_word]
            print(" ".join(map(str, indices))) if indices else print(-1)
    
  • + 0 comments

    I tried to use defaultdict.

    Following is my code:

    import sys from collections import defaultdict

    if name == 'main': n, m = map(int, input().split()) #input range check if n<0 or n>10000: print("Input from 1 to 10000!!!") sys.exit(-1) elif m<0 or m>100: print("Input from 1 to 100!!!") sys.exit(-1)

    #list/defaultdict definition
    a_list=[]
    b_list=[]
    A_indices=defaultdict(list)
    
    #input group A list
    for i in range(n):
        word_a=input()
        if len(word_a)<1 or len(word_a)>100:
            print("Input word in length from 1 to 100!!!")
            continue
        else:
            a_list.append(word_a)
            #input A_indices dict with word index according to word
            A_indices[word_a].append(i+1)
            #print(A_indices)   
    
    #input group B list and check words in group A                    
    for _ in range(m):
        word_b=input()
        if len(word_b)<1 or len(word_b)>100:
            print("Input word in length from 1 to 100!!!")
            continue
    
        b_list.append(word_b)
    
        #check if word exists in group A list by using A_indices defaultdict
        indices = A_indices[word_b]
    
        #print indices in group A list for word
        if indices:
            print(" ".join(map(str, indices)))
        else:
            print("-1")
    
  • + 0 comments

    i found myself solving this without the need to use defaultdict (but i feel like it's not the most efficient solution):

    n , m = map(int, input().split())
    A= []
    B = []
    for i in range(n):
        A.append(input())
        
    for i in range(m):
        B.append(input())
    
    found = False
    for charB in B:
        for ind,charA in enumerate(A):
            if charB == charA:
                print(ind+1, end=" ")
                found = True
    
    if found:
        print()
        found = False
    else:
        print(-1)