리스트에서 특정 원소의 갯수 찾아내기 - python, bisect

이태혁·2020년 10월 15일
0
from bisect import bisect_left, bisect_right

def count_by_range(a, left_value, right_value):
	right_index = bisect_right(a, right_value)
	left_index = bisect_left(a, left_value)
	return right_index - left_index


a = [1, 2, 3, 3, 3, 3, 4, 4, 8, 9]

#값이 4인 데이터 개수 출력
print(count_by_range(a, 4, 4, )) 


#값이 [-1, 3] 범위에 있는 데이터 개수 출력
print(count_by_range(a, -1, 3))

"""결과
2
6
"""
profile
back-end, cloud, docker, web의 관심이 있는 예비개발자입니다.

0개의 댓글