We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Collections
- DefaultDict Tutorial
- Discussions
DefaultDict Tutorial
DefaultDict Tutorial
Sort by
recency
|
1087 Discussions
|
Please Login in order to post a comment
I know I didnt use defaultdict but I cannot figure out why this code is failing test cases 1,2,3 and 4. Can anyone help me figure out?
n,m = map(int,input().split()) A = list() B = list()
for i in range(n): A.append(input()) for i in range(m): B.append(input())
for i in range (m): count = 0 for j in range(n): if B[i] == A[j]: print(j+1, end = ' ') count += 1 if count == 0: print(-1) print()
from collections import defaultdict
x,y = input().split() B = []
A = defaultdict(list)
for i in range(int(x)): a= input() A[a].append(i+1)
for j in range(int(y)): b= input()
Using Walrus Operator
from collections import defaultdict
n,m = map(int,input().split())
a = defaultdict(list)
for i in range(n): a[input()].append(i+1)
for i in range(m): print(*a[x] if (x:=input()) in a else [-1])
from collections import defaultdict n,m,dd = map(int,input().split()),defaultdict(list)
for i in range(n): dd[input()].append(i+1)
for _ in range(m): print(' '.join(map(str,dd.get(input(),[-1]))))
The statement of this problem is so confusing... spent most of my time figure out where does the m and n come from..