"Python Learning" list

Source: Internet
Author: User

Recently prepared to learn Python system, in order to be able to find in time, first recorded.

First of all, what is called sequence, before in use, feel the concept of a vague, today deliberately read the next, the sequence is the most basic data structure in Python, each element of the sequence is assigned an ordinal, that is, the position of the element, also become an index. Similar to "array". Python contains 6 built-in sequences, namely lists, tuples, strings, Unicode strings, buffer objects, and Xrange objects.

1. The contents of the list are mutable (relative to tuples), generally using "[]" and "," such as m= [1,2,3,4]

2. List element operations:

(1) element Assignment: You can assign values to a list similar to array assignments, you can assign different types of values to elements in a list, but you cannot assign values to the location of a nonexistent element.

(2) Add elements: You can use List.append (obj), the list stands for lists, and obj represents the objects that need to be added to the end of the list.

(3) Delete element : del m[0]

(4) Shard Assignment: use the list () function frequently

A. If M=list (' Today's weather is cloudy '), [' Present ', ' Day ', ' Day ', ' Day ', ' chi ', ' many ', ' Cloud '],list () function can directly convert the string to a list.

B. If m=list (' AE '), m[1:]=list (' BCD '), thelist () function can replace the Shard with a sequence of unequal lengths to the original sequence .

C. If m=list (' good weather Today '), M[2:2]=list (' Anhui Province '), the list function can insert new elements anywhere without replacing any of the original elements.

D. Fragment assignment can be used for element deletion, the element to be deleted is set to NULL, and any element in a sequence can be deleted.

3. The list can be nested.

4. How to List

(1) list.append (obj), which adds a new object at the end of the list

(2)list.count (obj), which is used to count the number of occurrences of an element in a list, the list represents a listing, and obj represents the object that is counted in the list

(3) list.extend (seq), list for lists, seq for element list

The main difference from the addition of the sequence: the Extend () method modifies the extended sequence, and the original connection operation returns a completely new list

(4) list.index (obj), which is used to find the index position of the first occurrence of a value from a list.

(5) List.insert (index,obj), the list represents the table, and index represents the index position where obj needs to be inserted, and obj represents the object in the Insert list. Used to find the index position of the first occurrence of a value from the list.

(Shard assignment can also be implemented)

(6) List.pop (obj=list[-1]), the list represents the listing, and obj is the optional parameter that represents the object to remove the list element. Used to remove an element from the list (the last element by default), and returns the value of the element.

A.list.pop (), does not pass parameters, the last element is removed by default

B.list.pop (2), delete a[2] Element

The Pop method is the only list method that can modify both the list and the method element value (except none).

Note: Pop methods can implement a common data structure-stack, similar to stacking plates, LIFO (last in FIRSR out), LIFO. On the stack (push), Python does not have a stack method, but can use the Append method instead of the stack (POP).

(7) list.remove (obj), the list represents the listing, and obj is the object to be removed from the list. The first occurrence of a value in the list to be removed. is an original position element change method with no return value.

(8) List.reverse (), the list represents a listing, and the method does not need to pass in parameters. Used to reverse the elements in the list. is an original position element change method with no return value.

(9) List.sort (func), list stands for lists, Func is an optional parameter, and if parameters are specified, the method of the parameter is used to sort.

Sort the list and maintain the order of the original list, assigning the original list using slices, such as B = a[:]

(10) list.sorted ()to directly get the original list copy to sort

(11) list.clear (), the list represents a listing and does not require an incoming parameter. Used to empty the list.

(12) list.copy (), the list represents a listing and does not require an incoming parameter. For assignment lists, similar to a[:]

(13) Advanced Sorting, the sort method has two optional parameters, namely Key,reverse.

#len按字符串长度由短到长进行排序

#传入2个参数, sort by long to short string lengths

#排序后逆序

2018/4/22

"Python Learning" 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.