Python collection and HEAPQ module usage instructions

Source: Internet
Author: User
Tags iterable

A: Collection library collection

Python has some built-in data types, and the Collections module provides several additional data types:

1,namedtuple generates a tuple subclass that can use a name to access the content of an element

2,deque dual-ended queue, which accelerates the addition and rollout of objects from the other side

3,counter counter, mainly used to count

4,ordereddict ordered Dictionary

5,defaultdict a dictionary with default values

1) namedtuple named Ganso form, generally need to know what the meaning of each field in the meta-ancestor, you can use named meta-ancestor Namedtuple

    

Inherit the named tuples

    

2) Deque dual-ended queue

The biggest benefit of deque is the rapid addition and removal of objects from the head of the queue, such as Popleft () and Appendleft ()

Append () appendleft () pop () Popleft () Extend () Extendleft () rotate ()

1, the most used is to limit the queue length, to get the last value of the queue or a few values

      

2) Other usage of deque, can refer to the following

3) Counter counter

Elements returns an iterator that displays the number of repetitions of the element, if the number of times is less than 1, is ignored

Most_common get the most frequently occurring elements

Subtract the elements of a two element counter

        

      

      

      

4) Ordereddict ordered dictionary

5) Defaultdict Default dictionary

Two: HEAPD heap queue

    

Heapq.heappush (heap, item)

Push the value item onto the heap, maintaining the heap invariant.

Heapq.heappop (Heap)

Pop and return the smallest item from the heap, maintaining the heapinvariant. If The heap is empty,Indexerror is raised. To access Thesmallest item without popping it, use heap[0].

Heapq.heappushpop (heap, item)

Push item on the heap, then pop and return the smallest item from the heap. The combined action runs more efficiently than Heappush () followed by a separate call to Heappop ().

Heapq.nlargest (n, iterable, Key=none)

Return a list with the n largest elements from the dataset defined byiterable. Key, if provided, specifies a function of one argument that isused to extract a comparison key from each element in the IT Erable:key=str.lower equivalent to:sorted (iterable, key=key,reverse=true) [: n]

Heapq.nsmallest (n, iterable, Key=none)

Return a list with the n smallest elements from the dataset defined byiterable. Key, if provided, specifies a function of one argument that isused to extract a comparison key from each element in the IT Erable:key=str.lower equivalent to:sorted (iterable, Key=key) [: n]

      

      

Python collection and HEAPQ module usage instructions

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.