Several magical libraries in Python (not to be continued)

Source: Internet
Author: User

1.bisect: Use the dichotomy to find the appropriate insertion position in a sorted sequence.

>>>import bisect>>>l = [10,19,88,90]>>>bisect.bisect (l, a) >>>2   # The position suitable for insertion is 2bisect.bisect_left (l,  #如果已经在列表中存在) and returns to the left position Bisect.insort_left (l,22)  #插入

2.HEAPQ: Fully balanced binary tree, all nodes are smaller than their child nodes.

From HEAPQ import *from random Import *l = sample (Xrange (+), ten) heap = []print lfor i in L:heappush (heap, i) Print Heappo P (Heap)   #弹出的是列表里最小的.
Nlargest (5, L) returns the  ordered largest n elements from the list nsmallest (5, L) returns the smallest n elements

  

Several magical libraries in Python (not to be continued)

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.