python common method on the list

1. Create a list

As long as the comma-delimited data items using different brackets ([]) to enclose the subscript (subscript index) starts from 0, the last index of the element can be written -1

list=['1','2','3']

list=[]

2, is inserted into the table element

list.append () add an element to the end of list

list.insert (n-, '. 4' ) added element at the specified position, if the specified label does not exist, then it is added at the end

list1.extend (list2) merge two list list2 there are still elements

3, view a list of values

print (list) traverse the list

Equivalent to for i in list:

                      print i

print (list [n]) to access a list of values ​​of the subscript index, you can use the same form of characters taken brackets

print (list.count (xx)) to view the number of an element in this list, and if the change element does not exist, then return 0

print (list.index (xx)) find a small sign of this element, if there are multiple returns the first, if find an element that does not exist will complain

4, delete the values ​​in the table

list.pop () to delete the last element

list.pop (n) specified index, delete the specified elements, if you delete an element that does not exist will complain

list.remove (xx) a delete list elements inside, a plurality of the same elements, remove the first 

print (list.pop ()) returns a value

print (list.remove ()) None Return Value

del [n] list deleting an element corresponding to the specified index 

del list to delete the entire list, the list can not access the deleted

5, sorting and reverse

list.reverse () will reverse list

the list.sort () sorting, in ascending order by default

list.sort (reverse = True) Descending

Note: list there are strings, not the sort digital, sorting for the same type

6, the operation of the function list

1, len (list): a list of the number of elements 
2, max (list): returns a list of the maximum element 
3, min (list): returns a list of the minimum element 
4, list (seq): converts a list of tuples

5, enumerate usage (printing elements corresponding subscript)

At the same time remove the standard elements

7, list cycle and sliced

 1 cycle

for i in list:

       print i

If the loop directly for a list of time, then the value of each cycle are elements inside the list

2, sections (a method of argument list)

name [n: m] does not contain the value of that slice is a rear element (care regardless of tail)

name [: m] If the previous slice, then a default value, taken from the beginning

name [n:] If the Default behind slices then taken to the end

name [:] If all the defaults, take all

name [n: m: s] s: the number of steps taken spacer elements a

Is a positive step, taken from left to right

Step is negative, taken from right to left

Note: The same applies to the slice string, the string has subscript

8, a listing formula

That list of Formula List Comprehensions, Python is a very simple but powerful built-in can be used to create a list of the formula.

Example 1: To generate a list  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]can be used list(range(1, 11)): list = list (range (1,11 ))

Example 2: generating a[1x1, 2x2, 3x3, ..., 10x10]可以用   list = list (x * x for x in range (1,11)) 
Example 3: for loop back if the judge can also add, so that we can filter out only the even numbered squares: list = list (x * x for x in range (1,11 ) if x% 2 == 0)
Example 4: Use of two cycles, the whole arrangement can be generated:
list = [m + n for m in 'ABC' for n in 'XYZ']
print(list)

Guess you like

Origin www.cnblogs.com/zhaoqing-cao/p/11621329.html