Operation of the container type list python

List: using commas to separate collection, using the element by element in the parentheses indicates the element; elements in the list can store different types of data, but typically store the same data type;

1. Declare list:

# Declare a list: variable name = [element 1, element 2] 
List1 = [1,2,3,4,5, ' A ' , ' B ' , ' C ' , ' D ' , ' E ' ]

2. The list of values:

# List table index value by the way, index starting from 0; 
Print ( ' first result: ' , List1 [0])
 # can specify not only by the initial index value of the index, the index can also be declared ended, End left and right to follow the opening and closing index interval; 
Print ( ' second results: ' , List1 [0:. 5 ])
 # In addition there is an initial index end index parameter stepping; 
Print ( ' third results: ' , List1 [0: 5: 2]) 
capture operation: If the initial value exceeds the maximum length of the list, the result returned is empty list; overindexing not throw an exception;
taken if the index value is negative way, then, must be from left to right to follow the rules of the interception;

3. The value added of the list:

# Declare a list: variable name = [element 1, element 2] 
List1 = [1,2,3,4,5, ' A ' , ' B ' , ' C ' , ' D ' , ' E ' ]

# List Value added: 
list1.append (100 )
 Print (List1)
 # append a new list: 
List2 = [ ' QQQ ' , ' WWW ' , ' Eee ' ]
list1.append(list2)
print(list1)

# Increasing element at the specified position in the list of 
list1.insert (5,200)        # lower position marked increase elements 5 
Print (List1)

# Update the list up in the original list 
List2 = [ ' QQQ ' , ' WWW ' , ' Eee ' ]
list1.extend(list2)     
Print (List1) 
different append # and extend directly to the list of elements is 2 1 added to the list. 2 is a list and append the entire list into a list, List 1, into a two-dimensional list;

# Of + splicing two lists 
List2 = [ ' QQQ ' , ' WWW ' , ' Eee ' ]
list3 = List1 + List2
 Print (list3) 
# + using different numbers of stitching extend and are, + stitching returns a new list is updated and extend in the original list;

4. Reverse the list:

# Declare a list: variable name = [element 1, element 2] 
List1 = [1,2,3,4,5, ' A ' , ' B ' , ' C ' , ' D ' , ' E ' ]
 # trans A method of transfer list: 
List2 = List1 [:: --1 ]
 Print (List2)
 # reverse listing second method: 
list1.reverse ()
 Print (List1)
 # difference between the two: [:: --1] for after the original list operation returns a new list, and open up space to store the new list; 
# and reverse () method returns a list of actions for the original value is None, it is based on the reverse of the original list elemental, not to open up new again memory space

 5. Delete the elements of the list:

# Delete list elements, pop method: 
# If you do not specify the index, then delete the last element in the list, if the specified index is specified index element 
list1.pop ()
 Print (List1)
 # the Remove method: parameter is carried element value, represents the removal of specified element appears for the first time 
list1.remove (5 )
 Print (List1)
 # del specified index list name [index argument: index end value] 
del List1 [0: 4 ]
 Print ( List1)
 # Clear clear list of values 
List1.Clear ()
 Print (List1)

6. Return index list elements

= List1 [1,2,3,4,5, ' A ' , ' B ' , ' C ' , ' D ' , ' E ' ]
 # use index method, to find an index value of the element; if the element is found, throwing abnormal a ValueError 
Print (list1.index ( ' A ' ))

7. The number of elements in the list that appears statistics

# Number of elements in the list appear in the statistics: 
Print (list1.count ( ' A ' ))

 

 

 

 

Guess you like

Origin www.cnblogs.com/XhyTechnologyShare/p/11845035.html