from collections import Counter
def solution(participant, completion):
participant = Counter(participant)
completion = Counter(completion)
result = list((participant-completion).keys())[0]
return result
participant
와 completion
배열의 요소 개수 파악-
사용collections 모듈의 Counter 클래스
Counter 클래스는 배열의 요소 개수를 Dictionary 형태로 반환한다.
from collections import Counter
x = ['a', 'a', 'b', 'b', 'b']
print(Counter(x)) // {'a': 2, 'b': 3}
-
는 개수가 0이면 key 값도 함께 제거한다.
from collections import Counter
x = ['a', 'b', 'c']
y = ['a', 'b']
print(Counter(x) - Counter(y)) // Counter({'c': 1})
subtract
은 개수가 0이어도 key 값을 유지한다.
from collections import Counter
x = ['a', 'b', 'c']
y = ['a', 'b']
print(Counter(x).subtract(Counter(y)))
// Counter({'a': 0, 'b': 0, 'c': 1})
from collections import Counter
def solution(participant, completion):
par = Counter(participant)
com = Counter(completion)
result = list((par - com).keys())
return result[0]