Algorithm

[Programmers/ Python] 완주하지 못한 선수

insub4067 2021. 11. 30. 09:30

개념은 쉽다

participant에서 completion을 빼주어

차집합을 찾으면 된다.

따라서 set를 이용해 해결하려 했으나

set는 중복된 값을 허용하지 않기 때문에 막혔다.

 

그래서 구글을 뒤지다가 collections.counter라는 모듈을 발견!

 

counter이란?

 

list안에 같은 이름을 가진 요소가 몇개 인지 

dict 형태로 알려준다.

예를 들면

import collections

participant = ["leo", "kiki", "eden"]
completion = ["eden", "kiki"]

p = collections.Counter(participant)
c = collections.Counter(completion)

print(p, c)

=> Counter({'leo': 1, 'kiki': 1, 'eden': 1}) Counter({'eden': 1, 'kiki': 1})

그럼 이상태에서 단순하게 p - c를 해주게 되면

남는 key 와 value를 알려준다

print(p - c)

=> Counter({'leo': 1})
# 여기서 부터는 데이터 가공해서 반환해 주기만 하면 된다

print(list(p - c)[0])

=> leo