Home > Backend Development > Python Tutorial > How to use bisect in Python

How to use bisect in Python

高洛峰
Release: 2016-12-14 15:33:29
Original
1584 people have browsed it

The analysis is as follows:

Generally speaking, bisect in Python is used to operate sorted arrays. For example, you can sort while inserting data into an array. The following code demonstrates how to operate:

import bisect
import random
random.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
Copy after login

The output result is:

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]
Copy after login

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

import bisect
import random
random.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
Copy after login

The output result is:

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]
Copy after login

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

I hope this article will be helpful to everyone’s Python programming.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template