List and metadata of python Data Structures

Source: Internet
Author: User

List and metadata of python Data Structures

Python data structure list and metadata

Sequence: a sequence is a data structure where all elements are numbered (starting from 0 ). Typical sequences include lists, strings, and metadata. The list is variable (which can be modified), while the tuples and strings are immutable (fixed once created ). The sequence contains six built-in sequences, including list, tuples, strings, Unicode strings, buffer objects, and xrange objects.

Declaration of the list:

mylist = []

2. List operations:

(1) sequence fragment: Usage: mylist [startIndex: endIndex: step] exam: mylist [] to retrieve 2nd to 10th characters. The default step is 1. mylist [] retrieves 2nd to 10th characters, and the specified step is 2. mylist [-2:-] The positive index is the coordinate relative to the header, and the negative value is the coordinate relative to the tail. In fact, the coordinates must be less than the ending coordinates. Otherwise, an empty part is returned. Mylist [-12:-2:-2] Step Size can also be a negative number, indicating to extract elements from the right to the left. (2) sequence index: Usage: mylist [index] exam: mylist [2] mylist [-2] ps: positive is the coordinate relative to the header, and negative is the coordinate relative to the tail. (3) sequence addition: Usage: mylist1 + mylist2 <=> [1, 2] + [3, 4] (4) sequence multiplication: Usage: mylist * 5 mylist element repeated 5 times. (5) in OPERATOR: Usage: 'item' in mylist determines whether mylist contains a member.

3. List-related built-in functions: built-in functions len, min, and max are very useful for list operations.

(1) The len function returns the number of elements contained in the sequence. (2) min and max functions return the largest and least elements in the Learning column respectively. (3) The list function converts a string to a list. Exam: list ('hello') => ['h', 'E', 'l', 'l', 'O'] (4) cmp function is used to compare the size of two elements exam: cmp (x, y) => returns 0 to indicate equal,-1 to x <y 1 to x> y (5) the reversed function performs reverse iteration on the sequence. (6) sorted returns a list of sorted elements including seq.

4. list method:

(1) append: The append method appends a new object to the end of the list. Exam: lst = [1, 2, 3] lst. append (4) => [1, 2, 4] (2) count: count the number of times an element appears in the list. Exam: x = [[], [, [] x. count (1) => 1 (3) extend: The extend method can append multiple values of another sequence at the end of the list. That is, you can use the new list to expand the original list. Exam: a = [1, 2, 3] B = [4, 5, 6]. extend (B) => [,] (4) index: The index method is used to locate the index location of the first matching item of a value from the list. Exam: lst = ['we', 'le', 'at'] lst. index ('le') => 1 (5) insert: The insert method is used to insert an object to the list: exam: lst = [1, 2, 3, 4, 5, 6] lst. insert () => [, 6] (6) pop: The pop method removes an element from the list (the last one by default) and returns this element. (7) remove: The remove method removes the first matching item of a value in the list: exam: x = ['to', 'be', 'or'] x. remove ('to') => you know. (8) The reverse Method sorts the elements in the list in reverse order. (9) The sort method is used to sort the list in the original position. Exam: the sort method has the default sorting method and advanced sorting usage. The sort method has two optional parameters: key, reverse, and key, the specified sorting is sorted by the key size, and the reverse is used to specify whether the sorting is reverse. X. sort (key = len) => indicates sorting by string length. X. sort (reverse = True) => indicates reverse sorting. X. sort (cmp) => specifies the sorting function, you know.

5. tuples: like lists, tuples are also a sequence. The only difference is that the tuples cannot be modified.

(1) declaration method: Use commas to separate some values, and the tuples are automatically created. Exam: 1, 2, 3 => (1, 2, 3) can also be declared through garden brackets. Exam: (, 3) => (, 3) (2) multiplication of tuples: 3*(40 + 2) => (, 42)

6. built-in functions involved in tuples:

(1) The functions of the tuple function are basically the same as those of the list function: Use a sequence as a parameter and convert it to a tuples. Exam: tuple ([1, 2]) => tuple (1, 2, 3)

7. shard of tuples:

exam: x=1,2,3 x[1] => 2 x[0:2] => (1,2)

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

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.