Python module introduction-bisect-ordered list

Source: Internet
Author: User

Python module introduction-bisect # undertake software automation implementation and training and other gtalk: ouyangchongwu # gmail.com qq 37391319 blog: http://blog.csdn.net/oychw # Shenzhen test automation python Project Access Group 113938272 Shenzhen accounting software test part-time job 6089740 # Shenzhen spread group 66250781 wugang cave Chengbu Xinning township sentiment group 49494279 # automated testing and python Group: http://groups.google.com/group/automation_testing_python? :::: the Python Standard Library by Example 2011 2.4 bisect-maintain the ordered list. Objective: you do not need to call sort to maintain the ordered list every 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. 2.4.1 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): r = random. randint (1,100) position = bisect. bisect (l, r) bisect. insort (l, r) print '% 3d % 3d' % (r, posit Ion), l execution result :#. /bisect_example.pyNew 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, 77, 79, 84, 85] the functions provided by the Bisect module are: bisect. bisect_left (a, x, lo = 0, hi = len (a): 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 (a) bisect. bisect (a, x, lo = 0, hi = len (a) is similar to bisect_left, but if x already exists, insert it on the right. Bisect. insort_left (a, x, lo = 0, hi = len (a) insert x in 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 (a) bisect. insort (a, x, lo = 0, hi = len (a) is similar to insort_left, but if x already exists, insert it to 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. There is an example of rating calculation based on scores in the standard: >>> 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 the following processing:> 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)

Related Article

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.