2010-07-07 9 views
16

정렬 된 순서로 검색을 수행 할 수있는 Python 빌트인 또는 널리 사용되는 파이썬 라이브러리가 있습니까?정렬 된 목록 검색?

+1

무엇의 순서? 또한 어떤 종류의 검색 (바이너리 등)입니까? –

+0

"정규"또는 "일반"이 되려고하는 질문에 "시퀀스"의 의미는 [시퀀스의 Python 문서 정의 (즉, Python 2.x "를 사용하는 것일 수 있습니다."일곱 가지 시퀀스 유형이 있습니다. 문자열, 유니 코드 문자열, 목록, 튜플, bytearrays, 버퍼 및 xrange 개체 ")] (https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple -bytrayray-buffer-xrange) –

답변

22

bisect은 표준 라이브러리의 일부입니다. - 찾고있는 것입니까?

+0

은 목록에서 값을 검색하는 방법을 설명하지 않습니다. –

13

sortedcontainersblist과 같은 빠른 검색을 구현하는 정렬 된 목록을 유지 관리하기위한 몇 가지 고품질 Python 라이브러리가 있다는 점은 주목할 가치가 있습니다. 이것들을 사용하는 것은 목록에서 요소를 삽입/제거하고 검색해야하는 빈도에 따라 다릅니다. 각 모듈은 정렬 순서대로 항목을 효율적으로 유지하는 SortedList 클래스를 제공합니다. SortedList에 대한 문서에서

는 :

L.bisect_left(value) 
    Similar to the bisect module in the standard library, this returns 
    an appropriate index to insert value in L. If value is already present 
    in L, the insertion point will be before (to the left of) any existing 
    entries. 

L.bisect(value) 
    Same as bisect_left. 

L.bisect_right(value) 
    Same as bisect_left, but if value is already present in L, the 
    insertion point will be after (to the right of) any existing entries. 

모두 구현은 주어진 값의 정확한 인덱스를 찾는 이진 검색을 사용합니다. 두 모듈 중 하나를 선택하기위한 페이지는 performance comparison입니다.

면책 조항 : 나는 sortedcontainers 모듈의 저자입니다.