https://programmers.co.kr/learn/courses/30/lessons/42576
def solution(participant, completion):
participant.sort()
completion.sort()
for i in range(len(completion)):
if participant[i] != completion[i]:
return participant[i]
return participant[-1]
import collections
def solution(participant, completion):
answer = collections.Counter(participant) - collections.Counter(completion)
return list(answer.keys())[0]
🙄 class collections.Counter([iterable-or-mapping])
📌 두 개의 Counter 간 덧셈, 뺄셈 가능
: 뺄셈의 경우 음수 값은 출력하지 않음
c = Counter(a=3, b=1)
d = Counter(a=1, b=2)
c + d
#결과: Counter({'a': 4, 'b': 3})
c - d
#결과: Counter({'a': 2})
📌 교집합(&), 합집합(|) 연산 가능
: 출력값은 {요소 : 개수}의 딕셔너리 형태로 반환
c = Counter(a=3, b=1)
d = Counter(a=1, b=2)
c & d
#결과: Counter({'a': 1, 'b': 1})
c | d
#결과: Counter({'a': 3, 'b': 2})
📌 most_common(n)
: 상위 n개 요소 추출
: n인자를 주지 않으면 전체 요소가 출력됨
: 가장 흔한 것부터 가장 적은 것 순으로 나열한 리스트를 반환
Counter('abracadabra').most_common(3)
#결과: [('a', 5), ('b', 2), ('r', 2)]
📌 elements()
: 반복되는 요소에 개수만큼 반환
: 요소는 처음 발견되는 순서대로 반환
: 요소의 개수가 1보다 작으면 elements()는 이를 무시
c = Counter(a=4, b=2, c=0, d=-2)
sorted(c.elements())
#결과: ['a', 'a', 'a', 'a', 'b', 'b']
📌 subtract()
: 말 그대로 요소를 빼는것을 의미
: 요소가 없는 경우는 음수의 값이 출력
c = Counter(a=4, b=2, c=0, d=-2)
d = Counter(a=1, b=2, c=3, d=4)
c.subtract(d)
c
#결과: Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})