[Python learning] list

Recently, I am going to systematically learn python. In order to find it in time in the future, I will record it first.

1. The content of the list is variable (relative to the tuple), generally use "[]" and ",", such as M= [1,2,3,4]

2. List element operation:

(1) Element assignment: List assignment can be performed similar to array assignment. Different types of values ​​can be assigned to elements in a list, but assignments cannot be made to the position of a non-existing element.

(2) Adding elements: you can use list.append(obj), list represents the list, and obj represents the object that needs to be added to the end of the list list.

(3) Delete element : del M[0]

(4) Fragment assignment: the list() function is often used

a. If M=list('The weather is cloudy today'), ['today','day','day','day','qi','more','cloud'], the list() function can be directly Convert string to list.

b. If M=list('ae'), M[1:]=list('bcd'), the list() function can replace the shards with a sequence that is not the same length as the original sequence .

c. If M=list('The weather is nice today'), M[2:2]=list('Anhui Province'), the list function can insert new elements at any position without replacing any original elements.

d. Element deletion can be performed by using fragment assignment, and the element to be deleted can be set to empty, and the element at any position in a sequence can be deleted.

3. Lists are nestable.

4. The method of lists

(1) list.append(obj), is to add a new object at the end of the list

(2) list.count(obj), used to count the number of times an element appears in the list, list represents the list, and obj represents the object counted in the list

(3) list.extend(seq), list represents a list, and seq represents a list of elements

 The main difference from adding a sequence: the extend() method modifies the extended sequence, and the original join operation returns a whole new list

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

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

(Shard assignment can also be implemented)

(6) list.pop(obj=list[-1]), list represents a list, and obj is an optional parameter, representing the object whose list elements are to be removed. Used to remove an element in the list (the last element by default) and return the value of that element.

a.list.pop(), no parameters, remove the last element by default

b.list.pop(2), delete the a[2] element

The pop method is the only list method that can both modify the list and method element values ​​(except None).

Note: The pop method can implement a common data structure - stack, similar to stacking dishes, LIFO (last in first out), last in first out. Push, python does not have a push method, but you can use the append method instead, pop.

(7) list.remove(obj), list represents a list, and obj is the object to be removed in the list. Used to remove the first occurrence of a value in a list. is a method for changing the element in the original position with no return value.

(8) list.reverse(), list represents a list, this method does not need to pass in parameters. Used to reverse the elements in a list. is a method for changing the element in the original position with no return value.

(9) list.sort(func), list represents a list, and func is an optional parameter. If a parameter is specified, the method of the parameter will be used for sorting.

对列表进行排序,且保持原列表排列顺序,先将原始列表使用切片方式进行赋值,如b = a[:]

(10)list.sorted(),直接获取原始列表副本进行排序

(11)list.clear(),list代表列表,不需要传入参数。用于清空列表。

(12)list.copy(),list代表列表,不需要传入参数。用于赋值列表,类似于a[:]

(13)高级排序,sort方法有两个可选参数,即key,reverse。

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

#传入2个参数,按照字符串长度由长到短进行排序

#排序后逆序

 

 

 

2018/4/22

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324645094&siteId=291194637