개발/Python
[파이썬] collections 모듈 - Counter (사용빈도 확인)
데이터의 개수를 셀 때 유용한 파이썬의 collections 모듈의 Counter 클래스 사용법을 알아보겠습니다. 알파벳 사용빈도 확인 from collections import Counter Counter('hello world') 실행 결과 Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1}) 다음으로는 most_common() 함수에 대해 설명하겠습니다. most_common()함수는 Counter()에서 가장 빈도수가 높은 순으로 표시 해 주는 함수입니다. 인자값으로 숫자를 입력하면 숫자번째까지의 빈도수를 표시합니다. from collections import Counter data="hello world" result..
[파이썬] 올림, 내림, 반올림
올림 import math math.ceil(-3.5) #결과 : -3 math.ceil(3.5) #결과 : 4 내림 import math math.floor(-3.5) #결과 : -4 math.floor(3.5) #결과 : 3 math.trunc(-3.5) #결과 : -3 math.floor(-3.5) #결과 : -4 trunc() 함수는 내림을 할 때 0쪽으로 향하는 반면(int()와 비슷) floor() 함수는 무조건 낮은 값으로 내림한다. 반올림 파이썬에 내장된 round() 함수를 사용한다. 두 개의 인자를 받지만, 두 번째 인자가 생략되면 소수 첫째 자리에서 반올림한다. round(3.123) #결과 : 3 round(3.123,2) #결과 : 3.12 ✔ 사사오입 원칙 round()는 사사오..