SoFunction
Updated on 2024-12-10

Learning Python with Laoqi's tolerant list(2)

Manipulation of lists

Merge list

The operations on lists in List(1) mention (x), which is the appending of an element x to the end of a known list.

In addition to appending elements to a list, it is also possible to merge two lists, or append one list to another. As usual, let's start by looking at the official documentation:

(L)
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
To all those who are studying this content, a must for becoming a good programmer: reading the official documentation is a must.

Translate this sentence from the official documentation:

Expanding a known list by appending all its elements to it is equivalent to a[len(a)] = L
English sucks, translation sucks. Look directly at the example, it's more understandable

>>> la
[1, 2, 3]
>>> lb
['qiwsir', 'python']
>>> (lb)
>>> la
[1, 2, 3, 'qiwsir', 'python']
>>> lb
['qiwsir', 'python']

The above example, shows how to take two lists, one with la and the other with lb, and append lb to the back of la, i.e., add all the elements in lb to la, i.e., make la expand.

You must be curious to learn programs, and I experiment a lot with my ideas, sometimes rather silly ones, in interactive environments.

>>> la = [1,2,3]
>>> b = "abc"
>>> (b)
>>> la
[1, 2, 3, 'a', 'b', 'c']
>>> c = 5
>>> (c)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

What can the watcher learn from the above experiment? It turns out that if extend(str) is done, str is broken up in characters and appended inside la.

An error is reported if the object being extended is of numeric type.

So the object of extend is a list, and if it's str, then python will convert it to a list by character before appending it to the known list.

However, don't forget the second half of the official documentation that precedes it:

>>> la
[1, 2, 3, 'a', 'b', 'c']
>>> lb
['qiwsir', 'python']
>>> la[len(la):]=lb
>>> la
[1, 2, 3, 'a', 'b', 'c', 'qiwsir', 'python']

(L) is equivalent to list[len(list):] = L, where L is the list to be merged into

Length of list

Remember how to get the length of str? What is its length? Can that method be used for lists? How does it work?

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'>

Conclusion:

len(x), works just as well for lists
The number of elements in the list is obtained
The return value is of type int
Number of elements in a list

The above len(L), you can get the length of the list, that is, how many elements in the list. python list has another operation, is to count how many times an element appears in the list, that is, how many elements there are in a certain element. This is what the official documentation says:

(x)
Return the number of times x appears in the list.
Be sure to keep experimenting to understand the refined expressions in the documentation.

>>> la = [1,2,1,1,3]
>>> (1)
3
>>> ('a')
>>> ('a')
>>> la
[1, 2, 1, 1, 3, 'a', 'a']
>>> ('a')
2
>>> (2)
1
>>> (5)   There is no 5 in #NOTE:la, but if you look for it this way, no error is reported and the number 0 is returned.
0

Position of the element in the list

The list of tolerant (1) "has been mentioned, you can list of elements, from left to right in order from 0 to establish the index (if from right to left, from -1 to start numbering), through the index can be extracted out of a certain element, or a certain number of elements. It is such as doing so:

>>> 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 reverse case, can we go through an element and find its number in the list?

The needs of the beholder are the direction of python, you think it, python does it.

>>> la
[1, 2, 3, 'a', 'b', 'c', 'qiwsir', 'python']
>>> (3)
2
>>> ('a')
3
>>> (1)
0
>>> ('qi')   #If it doesn't exist, it's an error
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ValueError: 'qi' is not in list
>>> ('qiwsir')
6

(x), where x is an element in the list, so that you are able to retrieve the position of that element in the list. This is the real index, notice that English word index.

Still the same official explanation as in the previous article:

(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.
Isn't that very clear and unambiguous?

That's all for now, we'll continue with the list.