# Bisect usage in Python and Pythonbisect usage

Source: Internet
Author: User

Bisect usage in Python and Pythonbisect usage

This article describes how to use bisect in Python. It is a common practical technique. Share it with you for your reference. The specific analysis is as follows:

In general, bisect in Python is used to operate the sorting array. For example, you can insert data into an array and sort it. The following code demonstrates how to operate:

`import bisectimport randomrandom.seed(1)print('New pos contents')print('-----------------')l=[] for i in range(1,15):  r=random.randint(1,100)  position=bisect.bisect(l,r)  bisect.insort(l,r)  print '%3d %3d'%(r,position),l`

Output result:

`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, 77, 79, 84, 85]`

We can see that the array is sorted at the same time when these random numbers are inserted. However, there are some repeated elements, such as the above 77 and 77. You can set the sequence of these repeated elements. If you want the repeated elements to appear on the left of the same element, you can use bisect_left. Otherwise, you can use bisect_right, and use insort_left and insort_right accordingly. For example, the following code shows repeated element index changes:

`import bisectimport randomrandom.seed(1)print('New pos contents')print('-----------------')l=[] for i in range(1,15):  r=random.randint(1,100)  position=bisect.bisect_left(l,r)  bisect.insort_left(l,r)  print '%3d %3d'%(r,position),l`

Output result:

`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  8 [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]`

This function is bisect. bisect (list, key), which is like the tailMap (fromkey) of TreeMap in java ).

Usage of help () in Python

() Is the name of the mod you want to query

How to use open () in python?

R indicates read-only, B Indicates binary
W indicates writability, and t indicates opening in text mode.
Add some official explanations:
>>> Print file. _ doc __
File (name [, mode [, buffering])-> file object

Open a file. The mode can be 'R', 'w' or 'A' for reading (default ),
Writing or appending. The file will be created if it doesn't exist
When opened for writing or appending; it will be truncated when
Opened for writing. Add a 'B' to the mode for binary files.
Add a' + 'to the mode to allow simultaneous reading and writing.
If the buffering argument is given, 0 means unbuffered, 1 means line
Buffered, and larger numbers specify the buffer size. The preferred way
To open a file is with the builtin open () function.
Add a 'u' to mode to open the file for input with universal newline
Support. Any line ending in the input file will be seen as a' \ N'
In Python. Also, a file so opened gains the attribute 'newlines ';
The value for this attribute is one of None (no newline read yet ),
'\ R',' \ n', '\ r \ n' or a tuple containing all the newline types seen.

Related Keywords:
Related Article

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.