Bisect module maintains an ordered list

Source: Internet
Author: User
Bisect-maintenance order list objective: to maintain the order list without calling sort each time. Bisect-maintain an ordered list

Objective: to maintain an ordered list without calling sort each time.

The bisect module implements an algorithm for inserting elements into an ordered list. In some cases, this is more efficient than repeatedly sorting a list or constructing a large list and then sorting. Bisect is the meaning of the binary sorting. here we use the binary sorting. the source code of bisect is a sample of the binary sorting. The code of this module contains less than 100 lines.

Insert

Import bisectimport random # Use aconstant seed to ensure that # the samepseudo-random numbers # are usedeach time the loop is run. random. seed (1) print 'new Pos contents' print '--- --------' # Generaterandom numbers and # insert theminto a list in sorted # order. l = [] for I inrange (1, 15): # Generate a random number from 1 to r = random. randint (1,100) position = bisect. bisect (l, r) bisect. insort (l, r) print '% 3d % 3d' % (r, position), l

Execution result:

#./Bisect_example.py

New Pos Contents

--------------

14 0 [14]

85 1 [14, 85]

77 1 [14, 77, 85]

26 1 [14, 26, 77, 85]

50 2 [14, 26, 50, 77, 85]

45 2 [14, 26, 45, 50, 77, 85]

66 4 [14, 26, 45, 50, 66, 77, 85]

79 6 [14, 26, 45, 50, 66, 77, 79, 85]

10 0 [10, 14, 26, 45, 50, 66, 77, 79, 85]

3 0 [3, 10, 14, 26, 45, 50, 66, 77, 79, 85]

84 9 [3, 10, 14, 26, 45, 50, 66, 77, 79, 84, 85]

44 4 [3, 10, 14, 26, 44, 45, 50, 66, 77, 79, 84, 85]

77 9 [3, 10, 14, 26, 44, 45, 50, 66, 77, 77, 79, 84, 85]

1 0 [1, 3, 10, 14, 26, 44, 45, 50, 66, 77, 79, 84, 85]


The Bisect module provides the following functions:

Bisect. bisect_left (a, x, lo = 0, hi = len ()):

Search for the index of x inserted in ordered list. Lo and hi are used to specify the range of the list. The entire list is used by default. If x already exists, insert it on the left. The return value is index.

Bisect. bisect_right (a, x, lo = 0, hi = len ())

Bisect. bisect (a, x, lo = 0, hi = len ())

The two are similar to bisect_left, but if x already exists, insert it on the right.

Bisect. insort_left (a, x, lo = 0, hi = len ())

Insert x into ordered list. The same effect as a. insert (bisect. bisect_left (a, x, lo, hi), x.

Bisect. insort_right (a, x, lo = 0, hi = len ())

Bisect. insort (a, x, lo = 0, hi = len ())

Similar to insort_left, but if x already exists, insert it on the right.

Functions can be divided into two types, bisect *, used to find the index. Insort * is used for actual insertion. By default, duplicates are inserted from the right. The actual common estimation is insort.

The standard has an instance that calculates the rating based on the score:

>>> Def grade (score, breakpoints = [60, 70, 80, 90], grades = 'fdba '):

... I = bisect (breakpoints, score)

... Return grades [I]

...

>>> [Grade (score) for score in [33, 99, 77, 70, 89, 90,100]

['F', 'A', 'C', 'C', 'B', 'A', 'A']

Bisect does not support keyword parameters like sort. we recommend that you handle the following:

>>> data =[('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]>>> data.sort(key=lambdar: r[1])>>> keys =[r[1] for r in data]         #precomputed list of keys>>> data[bisect_left(keys,0)]('black', 0)>>> data[bisect_left(keys,1)]('blue', 1)>>> data[bisect_left(keys,5)]('red', 5)>>> data[bisect_left(keys,8)]('yellow', 8)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.