Basic introduction to Python 0 (7)-------List

Source: Internet
Author: User

List lists belong to mutable list

(1) can contain an ordered set of arbitrary objects, "such as any data type, in the same order as we have entered"

(2) Access by index in the following table, as shown, can even be accessed with negative numbers-1 is the last element

Access range: From index 0 to index 2. after the index is not 2, please note the following

(3) variable length. Append method to append

(4) arbitrary nesting

(5) support for in-situ changes can be seen from 25 into

In memory. The person variable is a reference to a block, and then refers to the specific value that is pointed to.

When person[1] = 26; Then the memory reference will re-point to a new object

(6) An array of object references. The same object as "Mike" and x[1 "can see the y variable"

General operations for list "The following applies to other lists"

(1) List function, convert a value to list

(2) Determine if the element is inside the set "(in), (not in)"

(3) Join sequence List1 + List2 "List1,list2 itself without any change"

(4) Repeating sequence content List1 * N (n means repeating several times), "List1 itself has not changed"

(5) subscript Gets the element and accesses the specified index range

It says that the index after the range access is not included in the result, the following illustration explains

Range access can be thought of as the position of the index in the gap. Index access 0-2 Index, worth out of the top 2

See also the index of a negative number in the graph. We can also access the range through a negative index.

Range Access Cities[:2] is not written before, default starting from 0.

Cities[1:] Do not write behind, access to the last

cities[:] Do not write, access all

(6) Access to the specified index according to the step value syntax: List1[i:j:k]

K is the step value. K = 2. That is, fetch 1 per 2. (Each k take 1, last dissatisfied K, when K take)

(7) Gets the sequence length. Len function

(8) Some general functions of the list

Max Value (max)

Min value (min)

SUM (SUM)

gets the following table for an element List.index (x)

Gets the number of occurrences of an element in the list List.count (x)

Common operations for variable sequences and lists

(1) Change the value of an element list[0] = X

(2) Change the value within a range list[n:m] = t. T represents a value that can be iterated, and the number must be consistent with the scope of the substitution

(3) Delete element Del. Another way to delete a range x[0:2] = [] equals an empty sequence is also a delete

(4) List.remove (x) deletes a value. If the same value is the same, the first matching value is deleted

(5) List.clear () empty list

(6) Append element list.append (x) and extension element S.extend (list)

(7) List.insert (index,value) index position, value specific value. Inserts a value at the specified position

List.insert (0, 3.14) equals list[0:0] = [3.14]

(8) List.pop (index) deletes the element of the specified index and returns the deleted value.

(9) List.reverse () reversal. Does not return a value. It's changing the position of the element itself.

(10) Copy sequence

List.copy ()

y = list.copy () equals y = list[:]

(11) Sort List.sort () No return value, also affects itself

Reverse List.sort (key=none,reverse=true) parameter reversal

The sorted (list) returns a new result. does not affect itself

Extension: The key parameter of the sort method is a lambda. The following meaning is sorted according to the first letter

Basic introduction to Python 0 (7)-------List

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.