Python Data structure

Source: Internet
Author: User

Python data structures are mainly: list lists,

1, List

(1) Basic methods

Print("lsit List of PYTHON data Structures") A=[66.34,666,666,1,25.6]Print("list.count (x) returns the number of occurrences of x in the list:", A.count (666), A.count (66.34), A.count ('x')) A.insert (2,777)Print("List.insert (i, X) is inserted before I x:", a) a.append (888)Print("list.append (x) is the addition of an element to the end of the list:", a)Print("List.index (x) is the index of the element that returns the first value of x in the list. If there are no matching elements, an error is returned:", A.index (666)) A.remove (666)Print("List.remove (x) is the first element in the delete list that has a value of x. If there are no such elements, an error is returned:", a) a.reverse ()Print("List.reverse () is an element in the inverted list:", a) a.sort ()Print("List.sort () is the ordering of the elements in the list"A

Note: Methods such as Insert, remove, or sort modify the list without returning a value.

The result is:

(2) List lists can be used as stacks (LIFO)

Use the Append () method to add an element to the top of the stack.

A pop () method that does not specify an index can release an element from the top of the stack.

>>> stack=[2,3,4,5]>>> stack.append (6)>>> stack.append (7) >>> stack[2, 3, 4, 5, 6, 7]>>> stack.pop ()7>>> stack[2, 3, 4, 5, 6]>>> stack.pop ()6>>> stack.pop ()5>>> stack[ 2, 3, 4]

(3) List can be used as queue (FIFO)

The list can be used as a queue, but only the first element in the queue, the first to be taken out, but it is inefficient to use the list for such purposes. Adding or ejecting elements at the end of the list is fast, but it is not easy to insert or eject from the head at the table (because all other elements move one by one).

>>> fromCollectionsImportdeque>>> Queue=deque (["Zero"," One"," Both","three"])>>> Queue.append (" Four") # Four arrives>>> Queue.append ("Five") # Five arrives>>>Queuedeque (['Zero',' One',' Both','three',' Four','Five'])>>>queue.popleft () # The first to arrive now leaves'Zero'>>>Queue.popleft () # The second to arrive now leaves' One'>>>Queue # Remaining queue in order of Arrivaldeque ([' Both','three',' Four','Five'])

Python Data structure

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.