The bisect of the Python module

Source: Internet
Author: User

Python is an interesting module, bisect, it feels interesting, how interesting, below to give you the word.

Our husband becomes a list

data=[4,8,7,1]data.sort ()

Print this list

[1,4,7,8]

Import bisect

Import  Bisect

Inserting data

Bisect.insort (data,3)

Print data

[1, 3, 4, 7, 8]

Visible after inserting does not break the original list sort

Let's use the collar function again.

Print (Bisect.bisect (data,2))

Printing 1

Here is the location where the print is inserted. But it's not really plugged in.

The Bisect_left and Bisect_right functions, which are used to insert duplicate values, return where they will be inserted

Print (Bisect.bisect_left (data,2)) Print (Bisect.bisect_right (data,2))
Results:
11

is not inserted, but

Insort_left and Insort_right are plugged in.

Bisect.insort_right (data,2) bisect.insort_left (data,2)

Execution results

[1, 2, 2, 4, 7, 8]

As a result, insertions are not the same.

Open Source Address: https://github.com/liwanlei/

contact me: qq:952943386 email:[email protected] QQ Group: 194704520 new Group: 683894834

The bisect of the Python module

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.