-
[Programmers/ Python] 완주하지 못한 선수Algorithm 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
'Algorithm' 카테고리의 다른 글
[Programmers] 정렬 : K번째수 (0) 2022.01.27 [Programmers/ Python] 로또의 최고 순위와 최저 순위 (0) 2021.11.30 [Python] "피타고라스의 정리" Algorithm (0) 2021.10.30 백준 <1436번 : 영화감독 숌> (0) 2021.07.27 백준 <4673번 : 셀프 넘버> (0) 2021.07.27