Recently learning the Python language. Basically learn the basic syntax of Python. Feel that Python's position in data processing is inextricably linked to its list operation.
Learn about basic operations and take notes here.
' Python--version python 2.7.11quote:https://docs.python.org/2/tutorial/datastructures.html#more-on-listsadd by Camel97 2017-04 ' list.append (x) #在列表的末端添加一个新的元素Add a item to the end of the list; Equivalent to A[len (a):] = [x].
List.extend (L) #将两个 elements in the list are merged together
Extend the list by appending all the items in the given list; Equivalent to A[len (a):] = L.
List.insert (i, X) #将元素插入到指定的位置 (the position is the front one of the elements indexed to i)
Insert an item at a given position. The first argument is the index of the element before which to inserts, so A.insert (0, X) inserts at the front of the list, and A.insert (Len (a), X) are equivalent to A.append (x).
List.remove (x) #删除 the first element in the list with the value x (that is, if there are two X in the list, only the first x is deleted)
Remove the first item from the list whose value is X. It is a error if there is no such item.
List.pop ([i]) #删除 the first element in the list and returns this element. If you do not give parameter I, the last element in list will be deleted by default
Remove the item at the given position in the list, and return it. If No index is specified, A.pop () removes and returns the last item in the list. (The square brackets around the I in the method signature denote, the parameter are optional, not so you should type square brackets at that position. You'll see this notation frequently in the Python Library Reference.)
List.index (x) index of an element with a value of x #返回 list
Return the index in the list of the first item whose value is X. It is a error if there is no such item.
List.count (x) #返回 list, the number of elements with a value of X
Return the number of times X appears in the list.
Demo
#-*-coding:utf-8-*-l = [6] #添加print ll.extend (L2) #合并print ll.insert #创建 list L2 = [4,5,6]print ll.append] (0,0) #插入print Ll.remove (6) #删除print ll.pop () #删除print lprint l.index (2) #索引print L.count (2) #计数L. Reverse () # Reverse Print L
Result
[1, 2, 3] [1, 2, 3, 6] [1, 2, 3, 6, 4, 5, 6] [0, 1, 2, 3, 6, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5]21[5, 4, 3, 2, 1, 0]
List.sort (Cmp=none, Key=none, Reverse=false)
Sort the items of the list in place (the arguments can is used for sort customization, see sorted () for their explanation) .
1. Sort a list. By default in order from small to large
L = [2,5,3,7,1]l.sort () print l==>[1, 2, 3, 5, 7]l = [' A ', ' j ', ' G ', ' B ']l.sort () print l==>[' A ', ' B ', ' G ', ' J ']
2.reverse is a bool value. The default is False, and if you set it to True, the elements in the list will be sorted in reverse order of comparison.
# reverse is a Boolean value. If set to True, then the list elements is sorted as if each comparison were reversed.
L = [2,5,3,7,1]l.sort (reverse = true) print L==>[7, 5, 3, 2, 1]l = [' A ', ' j ', ' G ', ' B ']l.sort (reverse = true) Print l==>[ ' J ', ' G ', ' B ', ' a ']
3.key is a function that specifies the sort keyword, usually a lambda expression or a specified function
#key specifies a function of one argument that's used to extract a comparison key from each list element:key=str.lower. The default value is None (compare the elements directly).
#-*-coding:utf-8-*-#创建一个包含 tuple List of which the three elements of a tuple represent the name, height, age students = [(' John ', ","), (' Tom ') E ', students==>[,]print), (' John ', (+), (' Tom '), [' Dave ', ' Max '),]students.sort (key = Lambda Studen T:student[0]) Print students==>[(' Dave ', Max.), (' John ', [+]], (' Tom ', ' Max ')] #按名字 (first letter) sort Students.sort (key = L Ambda student:student[1]) print students==>[(' Tom ', (+), (' John '), (+), [' Dave ', ' Max ')] #按身高排序students. Sort (key = lambda student:student[2]) print students==>[(' Dave ', Max.), (' Tom ', ' Max '), (' John ', [[]]] #按年龄排序
4.CMP is a function that specifies two parameters. It determines the method of sorting.
#cmp Specifies a custom comparison function of the arguments (iterable elements) which should return a negative, zero O R positive number depending on whether the first #argument are considered smaller than, equal to, or larger than the second Argument:cmp=lambda x,y:cmp (X.lower (), Y.lower ()). The default value is None.
#-*-coding:utf-8-*-students = [(' John ', [+], +), (' Tom ', ' John, '), (' Dave ', ']print '), ' students==>[', 170, 1 5), (' Tom ', x, X), (' Dave ', Max.)] #指定 compare Students.sort (Cmp=lambda x,y:cmp () with the uppercase (ASCII) of the first letter and the lowercase (ASCII code) of the second letter ( X.upper (), Y.lower ()), key = Lambda student:student[0]) print students==>[(' Dave ', Max, ten), (' John ', 1 (+)] #指定 compare two-letter lowercase ASCII values Students.sort (Cmp=lambda x,y:cmp (X.lower (), Y.lower ()), key = Lambda student:student[0]) Print students==>[(' Dave ', [+], +), (' John ', [+], ')] #cmp (x, y) is a python built-in function to compare 2 objects, if x < y returns -1 if x = = y returns 0 if x > y returns 1
CMP allows the user to customize the size relationship. Usually we think 1 < 2, think a < B.
Now we can customize the function by customizing the size relationships (for example, 2 < A < 1 < b) to sort the list by the specified rules.
This is often useful when we are dealing with certain special problems.