List
This section continues with the discussion of the list of different tuples and strings: The list is variable (Mutable)----The list content can be changed, and the list has many useful and specialized methods.
ListA function can split a string into a list.
>>> List ( ' Chongshi ' )[ ' C ' , ' H ' , ' O ' , ' N ' , ' G ' , ' S ' , ' H ' ,' I ' ]
Change List: Element assignment
For example, we want to assign values to an element in a list.
>>> X = [1, 2, 3, 4]>>> X [2] = 18 >>>X [1, 2, 18, 4]
Delete Element
It is also easy to delete elements from the list.DeleStatement.
>>> Names = [ ' Zhangsan ' , ' Lisi ' , ' Wangwu ' , ' Sunliu ' ] >>> Del Names [2 ] >>> Names [ ' Zhangsan ' , ' Lisi ' , ' Sunliu ' ]
Partition assignment:
>>> Name = List ( ' Huzi ' ) >>> Name [ ' H ' , ' U ' , ' Z ' , ' I ' ] >>> Name [2:] = List ( ' Dazhi ' ) >>> Name [ ' H ' , ' U ' , ' D ' , ' A ' , ' Z ' , ' H ' , ' I ' ]
NameThe content is"Huzi", From3Characters (2) Start to replace.Da"Replace"Zi"And expanded"Zhi",So the newNameThe content is"Hudazhi".
List Method
A method is a function closely related to some objects. objects may be lists, numbers, strings, or other types of objects. Generally, a method can be called as follows:
Object. Method (parameter)
Method call is similar to function call.
1,Append
AppendThe method is used to append a new object to the end of the list:
>>> Abc = [1, 2, 3]>>> ABC. append (4)>>>ABC [1, 2, 3, 4]
2,Count
CountMethod to count the number of times an element appears in the list:
>>> [ ' To ' , ' Be ' , ' Or ' , ' Not ' , ' To ' , ' Be ' ,]. Count ( ' To ' ) 2 >>> x = [[], [, [ ] >>> X. Count (1 ) 2 >>> X. Count ([1, 2 ]) 1
3,Extend
ExtendYou can append multiple values in another sequence at the end of the list. Use the new list to expand the original list:
>>> A = [1, 2, 3]>>> B = [4, 5, 6]>>>A. Extend (B)>>>A [1, 2, 3, 4, 5, 6]
4,Index
IndexThe method is used to find the first matching item of a value from the list.
>>> Knights = [ ' We ' , ' Are ' , ' The ' , ' Kninghts ' , ' Who ' , ' Say ' , ' Ni ' ] >>> Knights. Index ( ' Who ' ) 4 >>> knights [4 ] ' Who '
5,Insert
InsertTo insert an object to the list:
>>> Numbers = [1, 2, 3, 5, 6, 7]>>> Numbers. insert (3,'Four')>>>Numbers [1, 2, 3,'Four', 5, 6, 7]
6,Pop
PopThe method removes an element from the list (the last one by default) and returns the value of this element:
>>> X = [1, 2, 3]>>>X. Pop ()3 >>>X [1, 2]>>>X. Pop (0)1 >>>X [2]
7,Remove
RemoveThe method is used to remove the first match of a value in the list:
>>> X = [ ' To ' , ' Be ' , ' Or ' , ' Not ' , ' To ' , ' Be ' ] >>> X. Remove ( ' Be ' ) >>>X [ ' To ' , ' Or ' , ' Not ' , ' To ' , ' Be ' ]
8,Reverse
ReversMethod to reverse store the elements in the list
>>> X = [1, 2, 3]>>>X. Reverse ()>>>X [3, 2, 1]
9,Sort
SortThe method is used to sort the list in the original position. In the "original position sorting", change the original list so that the elements can be arranged in a certain order.
>>> X = [4, 6, 2, 1, 7, 9]>>>X. Sort ()>>>X [1, 2, 4, 6, 7, 9]
Tuples: immutable Sequence
Like a list, tuples are also a sequence. The only difference is that the tuples cannot be changed. The syntax for creating tuples is simple: if you use commas to separate some values, you will automatically create tuples.
>>> 1, 2, 3(1, 2, 3)>>> (1, 2, 3)#Use parentheses to indicate tuples(1, 2, 3)>>> ()#Empty tuples()
How can we implement tuples containing a value? The method is a bit odd----A comma must be added, even if there is only one value:
>>> 42,(42,)>>> (42,)(42,)>>> 3*(20 + 1)63 >>> 3*(20 + 1,)(21, 21, 21)
TupleFunction
TupleFunctions andListFunctions are basically the same: Use a sequence as a parameter and convert it to a tuples.
>>> Tuple ([1, 2, 3]) (1, 2, 3)>>> Tuple ('ABC')('A','B','C')>>> Tuple (1, 2, 3))(1, 2, 3)