current position:Home>Python bisect module
Python bisect module
2022-01-31 18:03:25 【waws520】
bisect
—— This is a python For Insert and sort ordered arrays
A module of
First look at it. bisect There are some methods in this module
import bisect
[print(i) for i in dir(bisect)if i.find('__') == -1]
bisect
bisect_left
bisect_right
insort
insort_left
insort_right
Copy code
bisect It's called bisect_right
insort It's called Of insort_right
bisect
—— Actually bisect That's calling bisect_right
import bisect
li = [1, 23, 45, 12, 23, 42, 54, 123, 14, 52, 3]
li.sort()
print(li)
print(bisect.bisect(li, 3))
[1, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
Copy code
If you don't believe it, look at the source code
bisect = bisect_right # backward compatibility
Copy code
One sentence can explain
bisect_left(a, x, lo=0, hi=None)
—— The purpose is to find where the value will be inserted and return , Instead of inserting . If x It exists in a Middle returns x On the left
import bisect
li = [1, 23, 45, 12, 23, 42, 54, 123, 14, 52, 3]
li.sort()
print(li)
print(bisect.bisect_left(li, 3))
[1, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
Copy code
Source code is as follows
def bisect_left(a, x, lo=0, hi=None):
# a Original list
# x The elements inserted
# lo The starting position The default value is 0
# hi End position The default value is len(a)
# If the starting position is less than 0 False report
if lo < 0:
raise ValueError('lo must be non-negative')
# without End position be The default is The length of the list
if hi is None:
hi = len(a)
# Dichotomy
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x: lo = mid+1
else: hi = mid
# Return to location only
return lo
Copy code
bisect_right(a, x, lo=0, hi=None)
—— The purpose is to find where the value will be inserted and return , Instead of inserting . If x It exists in a Middle returns x On the right
import bisect
li = [1, 23, 45, 12, 23, 42, 54, 123, 14, 52, 3]
li.sort()
print(li)
print(bisect.bisect_right(li, 3))
[1, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
Copy code
Source code is as follows
def bisect_right(a, x, lo=0, hi=None):
# a Original list
# x The elements inserted
# lo The starting position The default value is 0
# hi End position The default value is len(a)
# If the starting position is less than 0 False report
if lo < 0:
raise ValueError('lo must be non-negative')
# without End position be The default is The length of the list
if hi is None:
hi = len(a)
# Dichotomy
while lo < hi:
mid = (lo+hi)//2
if x < a[mid]: hi = mid
else: lo = mid+1
# Return to location only
return lo
Copy code
insort
insort
—— In the list a Insert elements in x, And keep sorting after sorting . If x Already in a in , Insert it into the right x To the right of .
import bisect
li = [1, 23, 45, 12, 23, 42, 54, 123, 14, 52, 3]
li.sort()
print(li)
bisect.insort(li, 3.0)
print(li)
[1, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
[1, 3, 3.0, 12, 14, 23, 23, 42, 45, 52, 54, 123]
Copy code
- Actually insort That's calling insort_right
If you don't believe it, look at the source code
insort = insort_right # backward compatibility
Copy code
One sentence can explain
insort_left(a, x, lo=0, hi=None)
- In the list a Insert elements in x, And keep sorting after sorting . If x Already in a in , Insert it into the right x Left side .
import bisect
li = [1, 23, 45, 12, 23, 42, 54, 123, 14, 52, 3]
li.sort()
print(li)
bisect.insort_left(li, 3.0)
print(li)
[1, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
[1, 3.0, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
Copy code
Source code is as follows
def insort_left(a, x, lo=0, hi=None):
# a Original list
# x The elements inserted
# lo The starting position The default value is 0
# hi End position The default value is len(a)
# If the starting position is less than 0 False report
if lo < 0:
raise ValueError('lo must be non-negative')
# without End position be The default is The length of the list
if hi is None:
hi = len(a)
# Two points search
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x: lo = mid+1
else: hi = mid
# Insert
a.insert(lo, x)
Copy code
insort_right(a, x, lo=0, hi=None)
—— In the list a Insert elements in x, And keep sorting after sorting . If x Already in a in , Insert it into the right x To the right of .
import bisect
li = [1, 23, 45, 12, 23, 42, 54, 123, 14, 52, 3]
li.sort()
print(li)
bisect.insort_right(li, 3.0)
print(li)
[1, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
[1, 3.0, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
Copy code
Source code is as follows
def insort_right(a, x, lo=0, hi=None):
# a Original list
# x The elements inserted
# lo The starting position The default value is 0
# hi End position The default value is len(a)
# If the starting position is less than 0 False report
if lo < 0:
raise ValueError('lo must be non-negative')
# without End position be The default is The length of the list
if hi is None:
hi = len(a)
# Binary search
while lo < hi:
mid = (lo+hi)//2
if x < a[mid]: hi = mid
else: lo = mid+1
# Insert
a.insert(lo, x)
Copy code
copyright notice
author[waws520],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311803235228.html
The sidebar is recommended
- Python - convert Matplotlib image to numpy Array or PIL Image
- Python and Java crawl personal blog information and export it to excel
- Using class decorators in Python
- Untested Python code is not far from crashing
- Python efficient derivation (8)
- Python requests Library
- leetcode 2047. Number of Valid Words in a Sentence(python)
- leetcode 2027. Minimum Moves to Convert String(python)
- How IOS developers learn Python Programming 5 - data types 2
- leetcode 1971. Find if Path Exists in Graph(python)
guess what you like
-
leetcode 1984. Minimum Difference Between Highest and Lowest of K Scores(python)
-
Python interface automation test framework (basic) -- basic syntax
-
Detailed explanation of Python derivation
-
Python reptile lesson 2-9 Chinese monster database. It is found that there is a classification of color (he) desire (Xie) monsters during operation
-
A brief note on the method of creating Python virtual environment in Intranet Environment
-
[worth collecting] for Python beginners, sort out the common errors of beginners + Python Mini applet! (code attached)
-
[Python souvenir book] two people in one room have three meals and four seasons: 'how many years is it only XX years away from a hundred years of good marriage' ~?? Just come in and have a look.
-
The unknown side of Python functions
-
Python based interface automation test project, complete actual project, with source code sharing
-
A python artifact handles automatic chart color matching
Random recommended
- Python crawls the map of Gaode and the weather conditions of each city
- leetcode 1275. Find Winner on a Tic Tac Toe Game(python)
- leetcode 2016. Maximum Difference Between Increasing Elements(python)
- Run through Python date and time processing (Part 2)
- Application of urllib package in Python
- Django API Version (II)
- Python utility module playsound
- Database addition, deletion, modification and query of Python Sqlalchemy basic operation
- Tiobe November programming language ranking: Python surpasses C language to become the first! PHP is about to fall out of the top ten?
- Learn how to use opencv and python to realize face recognition!
- Using OpenCV and python to identify credit card numbers
- Principle of Python Apriori algorithm (11)
- Python AI steals your voice in 5 seconds
- A glance at Python's file processing (Part 1)
- Python cloud cat
- Python crawler actual combat, pyecharts module, python data analysis tells you which goods are popular on free fish~
- Using pandas to implement SQL group_ concat
- How IOS developers learn Python Programming 8 - set type 3
- windows10+apache2. 4 + Django deployment
- Django parser
- leetcode 1560. Most Visited Sector in a Circular Track(python)
- leetcode 1995. Count Special Quadruplets(python)
- How to program based on interfaces using Python
- leetcode 1286. Iterator for Combination(python)
- leetcode 1418. Display Table of Food Orders in a Restaurant (python)
- Python Matplotlib drawing histogram
- Python development foundation summary (VII) database + FTP + character coding + source code security
- Python modular package management and import mechanism
- Django serialization (II)
- Python dataloader error "dataloader worker (PID XXX) is killed by signal" solution
- apache2. 4 + Django + windows 10 Automated Deployment
- leetcode 1222. Queens That Can Attack the King(python)
- leetcode 1387. Sort Integers by The Power Value (python)
- Tiger sniffing 24-hour praise device, a case with a crawler skill, python crawler lesson 7-9
- Python object oriented programming 01: introduction classes and objects
- Baidu Post: high definition Python
- Python Matplotlib drawing contour map
- Python crawler actual combat, requests module, python realizes IMDB movie top data visualization
- Python classic: explain programming and development from simple to deep and step by step
- Python implements URL availability monitoring and instant push