List
This section continues the discussion of the list of different tuples and strings: The list is mutable (mutable)----can change the contents of the list, and the list has many useful, specialized methods.
The list function can split a string into a list.
>>> list (' Chongshi ') [' C ', ' H ', ' o ', ' n ', ' G ', ' s ', ' h ', ' I ']
Changing the list: assigning values to an element
For example, we want to assign a value 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 remove elements from the list, using the DELE statement.
>>> names = [' Zhangsan ', ' Lisi ', ' Wangwu ', ' Sunliu ']>>> del names[2]>>> names[' Zhangsan ', ' Lisi ', ' Sunliu ']
Shard Assignment :
>>> name = List (' Huzi ') >>> name[' h ', ' u ', ' z ', ' I ']>>> name[2:]=list (' Dazhi ') >>> name[' h ', ' u ', ' d ', ' a ', ' Z ', ' h ', ' I '
The content of name is "Huzi", starting with the 3rd character (2), replacing "Zi" with "Da" and being expanded "zhi", so the content of the new name is "Hudazhi".
List method
A method is a function that is closely related to some object, which can be a list, a number, or a string or other type of object, which in general is called by the method:
Object. Method (Parameter)
A method call is similar to a function call.
1, append
The Append 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
The Count method counts the number of occurrences of an element in the list:
>>> [' To ', ' is ', ' or ', ' not ', ' to ', ' is ',].count (' to ') 2>>> x = [[1,2],1,1,[2,1,[1,2]]]>>> X.count (1) 2>>> x.count ([up]) 1
3, Extend
The Extend method can append multiple values from another sequence at the end of the list at once. To extend an existing list with a new list:
>>> a = [1,2,3]>>> B = [4,5,6]>>> A.extend (b) >>> a[1, 2, 3, 4, 5, 6]
4. Index
The index method is used to find the first occurrence of a value from a list.
>>> Knights = [' We ', ' is ', ' the ', ' kninghts ', ' who ', ' say ', ' ni ']>>> knights.index (' Who ') 4>> > knights[4] ' Who '
5. Insert
The Insert method is used to insert an object into the list:
>>> numbers = [1,2,3,5,6,7]>>> Numbers.insert (3, ' four ') >>> numbers[1, 2, 3, ' Four ', 5, 6, 7]
6. Pop
The Pop method removes an element from the list (the last one by default) and returns the value of the element:
>>> x = [1,2,3]>>> x.pop () 3>>> x[1, 2]>>> x.pop (0) 1>>> x[2]
7. Remove
The Remove method is used to remove the first occurrence of a value in the list:
>>> x = [' To ', ' is ', ' or ', ' not ', ' to ', ' is ']>>> x.remove (' is ') >>> x[' to ', ' or ', ' not ', '-', ' be ' ‘]
8, Reverse
The Revers method stores the elements in the list in reverse
>>> x = [1,2,3]>>> x.reverse () >>> x[3, 2, 1]
9. Sort
The sort method is used to sort the list at the original location. "Sort in place" changes the original list so that the elements can be sorted in a certain order.
>>> x = [4,6,2,1,7,9]>>> x.sort () >>> x[1, 2, 4, 6, 7, 9]
Tuples: Immutable sequences
Tuples, like lists, are also a sequence. The only difference is that tuples cannot be changed. The syntax for creating tuples is simple: if you split some values with commas, you automatically create tuples.
>>> (1, 2, 3) >>> ( Square) # The tuple (1, 2, 3) >>> () # Empty tuple ()
How do you implement a tuple that contains a value? The method is a bit peculiar----must be comma-even if there is only one value:
>>> (21,) >>> (3) (20+1) 63>>> 3 * (20+1,) (21, 21,)
Tuple function
The function of the tuple function is basically the same as the list function: Take a sequence as a parameter and convert it to a tuple.
>>> tuple ([+ +]) (1, 2, 3) >>> tuple (' abc ') (' A ', ' B ', ' C ') >>> tuple ((+)) (1, 2, 3)
Python Basic Learning Notes (iv)