Operations on the list
Merge List
In the large list (1), the operation of List refers to List.append (x), which is to append an element x to a known list.
In addition to appending elements to the list, you can merge two lists, or append a list to another list. In accordance with the previous practice, you should first look at the description in the official documentation:
List.extend (L)
Extend the list by appending all the items in the given list; Equivalent to A[len (a):] = L.
Provide a must for all friends who are learning this content to be a good programmer: it is necessary to look at official documents.
The official document of this sentence translates:
Expand it by appending all elements to the known list, which is equivalent to A[len (a)]= L
English is too bad, translation is too bad. Look directly at the example, more 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 the two list, one for LA, and another lb, to append lb to the back of LA, that is, to add all the elements of lb to LA, that is, let la expand.
Learning procedures must be curious, I in the interactive environment, often experimenting with their own ideas, sometimes more foolish ideas.
>>> 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 "
", line 1, in
TypeError: ' int ' object
is not iterable
From the above experiment, crossing can have any experience? Originally, if extend (str), Str was opened in characters, and then appended to LA.
If the Extend object is numeric, an error is obtained.
So, the Extend object is a list, and if it is STR, Python will first convert it to a list by character and append to the known list.
But don't forget the second half of the 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 length of STR was obtained? What does its length contain? Can that method be used on the list? What's the effect?
Do the experiment:
>>> name = ' Qiwsir ' >>> type (name)
>>> len (name) 6>>> lname = [' Sir ', ' Qi ']>>> type (lname)
>>> len (lname) 2>>> length = Len (lname) >>> Length2>>> type (length)
Experimental conclusion:
Len (x), as applicable for list
The number of elements in the list is obtained
The return value is of type int
The number of elements in a list
Above Len (L), you can get the length of the list, that is, how many elements are in the list. The Python list also has an operation that counts how many times an element appears in the list, that is, how many elements there are. This is what the official document says:
List.count (x)
Return the number of times X appears in the list.
Always experiment 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: La does not have 5, but if you use this method to find, do not error, return is the number 00
The position of the element in the list
As already mentioned in the list (1), the list of elements can be numbered from left to right, starting from 0, and indexed (if right-to-left, numbering starts from 1), an index can extract an element, or some element. This is done 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 by an element?
Crossing need is the direction of Python, you think, Python will do.
>>> la[1, 2, 3, ' A ', ' B ', ' C ', ' Qiwsir ', ' Python ']>>> la.index (3) 2>>> la.index (' a ') 3>>& Gt La.index (1) 0>>> la.index (' qi ') #如果不存在, error traceback (most recent call last): File "
", line 1, In
valueerror: ' Qi ' isn't in list>>> la.index (' Qiwsir ') 6
List.index (x), X is an element in the list so that it can retrieve the position of the element in the list. This is the real index, note that the English word index.
is still the last official explanation:
List.index (x)
Return the index in the list of the first item whose value is X. It is a error if there is no such item.
Is that a very clear idea?
To get here first, we continue to have a big list.