자기개발👨‍💻/코딩 알고리즘

[python] 백준10816 숫자 카드2

천숭이 2021. 7. 12. 12:55

https://www.acmicpc.net/problem/10816

 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

from collections import Counter
import sys
a=int(sys.stdin.readline())
numbers=[]
numbers=list(map(int,sys.stdin.readline().split()))

b=int(sys.stdin.readline())
howmany=list(map(int,sys.stdin.readline().split()))

counter=Counter(numbers)
print(counter)
for i in howmany:
    print(counter.__getitem__(i),end=" ")

리스트 내부의 원소가 몇개 있는지 알려주는 라이브러리 Counter을 사용했다.

counter의 형태는 Counter({10: 3, 3: 2, -10: 2, 6: 1, 2: 1, 7: 1}) 딕셔너리 형태이다.

딕셔너리 원소(Key)가 아닌 원소의 갯수 (Value)를 출력하기 위해서는 getitem메소드를 사용해야한다.