파이썬 (2) 썸네일형 리스트형 [Python] Built-in round() 함수에 대해서 파이썬에서 반올림을 지원하는 내장 함수로 round() 함수가 있다. Java의 Math.round() 함수와 동일하게 동작하겠거니하고 알고리즘 문제에 사용했더니 예상된 결과값이 나오지 못했다. 바로 xxx.5 일 경우 파이썬의 round() 함수가 경우에 따라 다르게 동작하는 것이다.Q. 그럼 파이썬의 round() 함수는 .5에 대해 올림을 할까, 아니면 내림을 할까?정답은 "경우에 따라 둘 다 적용될 수 있다"파이썬의 round() 함수는 "Banker's Rounding", 혹은 "Round Half To Even” 이라는 방식을 따른다. 이 방식은 반올림해야 할 숫자가 정확히 .5 일 때, 가장 가까운 짝수 쪽으로 반올림한다.print(round(0.5)) # 0print(round(1.5)) .. max함수로 딕셔너리 내 최대 value에 대한 key 찾기 import collections import re paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." banned = ["hit"] words = [word for word in re.sub(r'[^\w]',' ',paragraph).lower().split() if word not in banned] counts = collections.defaultdict(int) for word in words: counts[word] += 1 print(max(counts, key=counts.get)) max(counts)를 하면 counts의 key값 중 최대값을 출력한다. counts.get()은 일반적으로 인자로 key값을 갖고 그.. 이전 1 다음