Action on List
Merge List
In the List.append list (1), the operation of the list refers to the X, which appends an element x to a known list.
In addition to appending the elements to the list, you can also merge the two list, or append a list to another list. As is customary in the previous article, first look at the description in the Official document:
List.extend (L)
Extend the list by appending all the items in the given list; Equivalent to A[len (a):] = L.
Provide a good programmer for all the friends who are learning this content: it is necessary to read the official document.
This sentence of the official document translates:
Expand it by appending all the elements to a known list, equivalent to A[len (a)]= L
English sucks, too bad translation. Just look at the example and understand
>>> La
[1, 2, 3]
>>> lb
[' qiwsir ', ' python ']
>>> la.extend (lb)
> >> La
[1, 2, 3, ' Qiwsir ', ' python ']
>>> lb
[' qiwsir ', ' python ']
The above example shows how to add a lb to the back of LA with two lists, one LA, another lb, and add all the elements in the LB to LA, allowing LA to enlarge.
Learn the procedure must have the curiosity, I in the interactive environment, often experiment own idea, sometimes is the more stupid idea.
>>> la = [1,2,3]
>>> b = "abc"
>>> la.extend (b)
>>> la
[1, 2, 3, ' a ', ' B ', ' C ']
>>> c = 5
>>> la.extend (c)
Traceback (most recent call last):
File "<stdi N> ", line 1, in <module>
typeerror: ' int ' object are not iterable
From the above experiment, reader can have what experience? Originally, if extend (str), STR was taken apart in characters and appended to LA.
If the Extend object is a numeric type, the error occurs.
So, the object of the extend is a list, and if it is str, then Python converts it to the list in characters and appends it to the known list.
But don't forget the second half of the previous official document, which means:
>>> La
[1, 2, 3, ' A ', ' B ', ' C ']
>>> lb
[' qiwsir ', ' python ']
>>> La[len ( LA):]=lb
>>> la
[1, 2, 3, ' A ', ' B ', ' C ', ' Qiwsir ', ' python ']
List.extend (L) is equivalent to List[len (list):] = l,l is the list to be merged
The length of the list
Remember how the str length was obtained? What is the length of the containing? Can you use that method on the list? What's the effect?
Do the experiment:
>>> name = ' Qiwsir '
>>> type (name)
<type ' str ' >
>>> len (name)
6
>>> lname = [' Sir ', ' qi ']
>>> type (lname)
<type ' list ' >
>>> len ( lname)
2
>>> length = Len (lname)
>>> length
2
>>> type (length)
<type ' int ' >
Experimental conclusion:
Len (x), as applicable for a list
Gets the number of elements in the list
return value is int type
Number of elements in a list
The Len (L) above can get the length of the list, which is the number of elements in the list. The Python list also has an operation that counts the number of times an element appears in the list, that is, how many elements there are. That's what the official document says:
List.count (x)
Return the number of times X appears in the list.
Be sure to experiment continuously to understand the refined expression in the document.
>>> la = [1,2,1,1,3]
>>> la.count (1)
3
>>> la.append (' a ')
>>> La.append (' a ')
>>> la
[1, 2, 1, 1, 3, ' A ', ' a ']
>>> la.count (' a ')
2
>>> La.count (2)
1
>>> la.count (5) #NOTE: There are no 5 in LA, but if you look for it in this way, no error, return the number 0
0
The position of the element in the list
It has already been mentioned in the list (1) of the large capacity, you can index the elements in the list from left to right, starting with 0, and indexing (if you start from right to left, numbering from 1), you can extract an element, or a few elements, from the index. It is as follows:
>>> La
[1, 2, 3, ' A ', ' B ', ' C ', ' Qiwsir ', ' python ']
>>> la[2]
3
>>> la[ 2:5]
[3, ' A ', ' B ']
>>> la[:7]
[1, 2, 3, ' A ', ' B ', ' C ', ' Qiwsir ']
If you consider the reverse, can you find the number in the list through an element?
Reader's need is the direction of Python, you think, Python does it.
>>> La
[1, 2, 3, ' A ', ' B ', ' C ', ' Qiwsir ', ' python ']
>>> la.index (3)
2
>>> La.index (' a ')
3
>>> la.index (1)
0
>>> la.index (' qi ') #如果不存在, the error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
valueerror: ' Qi ' isn't in Li St
>>> la.index (' Qiwsir ')
6
List.index (x), X is an element in the list, so you can retrieve the element's position in the list. This is the real index, pay attention to the English word index.
is still the last official explanation:
List.index (x)
Return the index in the list of the the ' the ' the ' the ' the ' the ' the ' the '. whose It is a error if there is no such item.
Is that very clear?
First come here, the next talk also continues to have a large list.