In the previous article, we mentioned list. append (x) for list operations, that is, to append an element x to a known list. Today, we will continue to discuss the list. List operations
Merge list
List. append (x) is mentioned in the list operation in "a large list (1)", that is, an element x is appended to a known list.
In addition to appending an element to the list, the two lists can also be merged, or a list can be appended to another list. Follow the previous conventions to see 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 all the friends who are learning this content with a must to become a good programmer: reading official documents is required.
This sentence in the official document is translated as follows:
Append all elements to a known list to expand it, which is equivalent to a [len (a)] = L
English is too bad and translation is too bad. Let's look at the example to better understand it.
>>> la[1, 2, 3]>>> lb['qiwsir', 'python']>>> la.extend(lb)>>> la[1, 2, 3, 'qiwsir', 'python']>>> lb['qiwsir', 'python']
The example above shows how to append the lb to the end of the two lists, one is la and the other is lb, that is, to add all the elements in the lb to la, expand la.
I must be curious when learning a program. in an interactive environment, I often experiment with my own ideas. sometimes I am stupid.
>>> 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
In the above experiment, what do you learn about the official website? Originally, if extend (str) is used, str is split in characters and then appended to la.
If the extend object is numeric, an error is returned.
Therefore, the extend object is a list. if it is str, python will first convert it to list by character and then add it to the known list.
However, do not forget the last 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.
List length
Do you still remember how to get the str length? What is its length? Can that method be used on the list? What is the effect?
Lab:
>>> name = 'qiwsir'>>> type(name)
>>> len(name)6>>> lname = ['sir','qi']>>> type(lname)
>>> len(lname)2>>> length = len(lname)>>> length2>>> type(length)
Experiment conclusion:
Len (x), applicable to the same list
The number of elements in the list is displayed.
The returned value is of the int type.
Number of elements in the list
The above len (L) can obtain the length of the list, that is, the number of elements in the list. The python list also has an operation, that is, how many times a certain element appears in the list, that is, how many elements there are. This is what the official documentation says:
List. count (x)
Return the number of times x appears in the list.
Continuous experimentation is required to understand the refined expression in the document.
>>> La = [1, 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 is No 5 in la, but if this method is used, no error is returned, and the number 00 is returned.
Position of an element in the list
As mentioned in "a large list (1)", the elements in the list can be numbered from left to right from 0 to create an index (if the index is from right to left, numbers starting from-1). an index can be used to extract an element or several elements. 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 we consider the opposite, can we find the number in the list through an element?
The need to look at the official site is the direction of python. you can think of it as python.
>>> La [1, 2, 3, 'A', 'B', 'C', 'qiwsir ', 'Python'] >>> la. index (3) 2> la. index ('A') 3 >>> la. index (1) 0 >>> la. index ('qi ') # if it does not exist, the error Traceback (most recent call last) is returned: File"
", Line 1, in
ValueError: 'QI' is not in list >>> la. index ('qiwsir ') 6
List. index (x), x is an element in the list, so that you can retrieve the position of this element in the list. This is the real index. pay attention to the index of the English word.
It is still the previous official explanation:
List. index (x)
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
Is it clear?
Here, we will continue to talk about the list with high storage capacity.