list in python

 
 
The data items of the list do not need to have the same type to create a list, as long as the different data items separated by commas are enclosed in square brackets, slices can also be used in the list. Data structure ---> list: add , delete, modify (insert, delete, update ,query) li1=[ 'a' , 'b' , 'c' , 'd' ] li2=[ 1 , 2 , 3 , 4 ] li1.append( 5 ) print (li1) [ 'a' , 'b ' , 'c' , 'd' , 5 ] append append to the end to generate a new subscript li1.insert( 2 , '#' ) print (li1) [ 'a' ,'b', '#' , 'c' , 'd' ] insert Insert characters according to subscripts If the length of the string exceeds the length, append to the end li1.append(li2) print (li1) [ 'a' , 'b' , 'c ' , 'd' , [ 1 , 2 , 3 , 4 ]] add li1.extend(li2) print (li1) [ 'a' , 'b' , 'c' , 'd' , 1 , 2 , 3 , 4 ] li1.extend(li2) iterates through the loop for iin li2: li1.append(i) print (li2* 3 ) [ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ] Modify li2[ 2 ]= 5 according to the subscript make character modification print (li2) [ 1 , 2 , 5 , 4 ] delete del pop remove del li1[ 1 ] print (li1) [ 'a'[ _ _ _ _ _ _ _ _ _ 1 , 2 , 3 ] li2.pop( 1 ) print (li2) [ 1 , 3 , 4 ] pop=li2.pop() print (li2) [ 1 , 2 , 3 ] li3=[ 1 , 2 , 3 , 4 , "*" , 4 ] [ 1 , 2 , 3, '*' , 4 ] remove only removes the first matching object of the same element, and the corresponding element is written in parentheses, not its subscript li3.remove( 4 ) print (li3) Check print (li3.index( 4 )) 3 Find the first occurrence of the same string, that is, its corresponding subscript is not found. Throws an exception nested list li4=[li1,li2] print (li4) [[ 'a' , 'b' , 'c' , 'd' ], [ 1 , 2 , 3 , 4 ]] li4=[li1,li2] print (li4) print (li4[ 1 ][ 2 ]) [[ 'a' ,'b' , 'c' , 'd' ], [ 1 , 2 , 3 , 4 ]] 3 where 3 found by li4[1][2] is the subscript list function print corresponding to the two nested lists ( dir ( list )) print all functions and attributes of a data type print (li4. __len__ ()) [[ 'a' , 'b' , 'c' , 'd' ], [ 1 , 2 , 3 , 4 ]] 2 print ( len (li4)) 2 print( max (li2)) 4 print ( min (li2)) 1 tu=( 7 , 8 , 9 ) new_li= list (tu) constructor --> open up a new space to initialize data print ( id (li1)) return variable object The memory address of print ( id (li2)) tu=( 7 , 8 , 9 ) new_li= list (tu) print (new_li) print (tu) [ 7 , 8 , 9 ] ( 7 , 8 , 9) li1.extend(tu) print (li1) [ 'a' , 'b' , 'c' , 'd' , 7 , 8 , 9 ] appends each value in another sequence new_li at once at the end of the list. append( 6 ) print (new_li) [ 7 , 8 , 9 , 6 ] appends values ​​at the end li1.reverse() print (li1) [ 'd' , 'c' , 'b' , 'a' ] Reverse li1 .sort() sorts the list, using the comparison function to compare li5=li1 if an argument is specified.copy() copies the list print(li5) ['a', 'b', 'c', 'd'] li5=li1.copy() li5[2]=7 print(li5) ['a', 'b', 7, 'd'] a=[1,2,3] b=a b指向a print(a) print(b) [1, 2, 3] [1, 2, 3] c=[] c=a c[1]=6 print(c) print(a) [ 1 , 6 , 3 ] [ 1 , 6 , 3 ] d=a[ : ] All values ​​fetch and assign print (d) [ 1 , 6 , 3 ] d=a[ : ] d[ 1 ]= 9 print (d) print (a) [ 1 , 9 , 3 ] [ 1 , 2 , 3 ]

Guess you like

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