Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 조맹크로키
- bootstrap4
- Python
- 알리
- 오류
- 크로키
- error
- ValueError
- JavaScript
- junny
- Django
- You should consider upgrading
- ModuleNotFoundError
- include
- scotty
- 조맹클래스101
- 취미미술
- Uncaught SyntaxError
- pip install --upgrade pip
- TemplateSyntaxError
- 블린이
- 마이그레이션
- navbar
- Migrate
- 마이그레이트
Archives
- Today
- Total
내가 하고 싶은 것들 중 하나
[프로그래머스] 완주하지 못한 선수 본문
아이콘 제작자 Skyclick from www.flaticon.com
from collections import Counter
def solution(participant, completion):
answer = ''
part_count = Counter(participant)
part_count.subtract(completion)
#for idx, value in part_count.items():
#if value == 1:
#answer += idx
answer = [key for (key, value) in part_count.items() if value == 1][0]
return answer
A와 B 리스트에서 중복되는 값을 지우고 남는 값이 무엇인지 구할 때
Counter class
의subtract
매서드를 이용하면 된다.collections
모듈에서Counter class
import
하기Counter
함수는 딕셔너리로 반환된다.Counter
에는subtract 매서드
가 내장되어 있다.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})
https://docs.python.org/3/library/collections.html#collections.Counter
- 새로 알게 된 것
A = [a, b, c, a]
B =[a, b, c]
>>>c = Counter(A)
>>>c.subtract(B) # B를 c와 같이 먼저 딕셔너리로 만들어주지 않고 리스트를 바로 넣어도 작동됨
>>>c
Counter({'a': 1, 'b': 0, 'c': 0})
'알고리즘' 카테고리의 다른 글
[프로그래머스] 숫자의 표현 (0) | 2020.07.15 |
---|
Comments