문제

 

내가 작성한 코드
from collections import Counter

def solution(participant, completion):
    result = Counter(participant) - Counter(completion)
    return list(result.keys())[0]

 

코드 설명
from collections import Counter

> collections 모듈에서 Counter 함수는 리스트 안의 같은 값의 수를 dict 형으로 변환시켜준다.

 

def solution(participant, completion):
    result = Counter(participant) - Counter(completion)
    return list(result.keys())[0]

> participant 변수와 completion 변수를 가각 Counter 함수를 적용시키고 빼주면, completion에서 없는 하나의 값을 뽑을 수 있따.

> Counter 함수와 마찬가지로 set 형에서도 차집합을 적용할 수 있다. 하지만, 리스트에서는 불가하다.

> 마지막으로 하나의 결과값이 {'완주하지 못한 선수' : 1} 형식으로 되어있는데, 여기서 선수 이름을 추출하면되므로 .keys() 를 적용하고 리스트로 바꿔준다음 [0]을 해서 해당 값을 추출해주면 된다.

+ Recent posts