1, Basic operations of list:
1 empty = []
2 list1 = ['jimei','ruanjian','2021','37']
3 list1[1] = 100#Modify element
4 print(list1)
5 del list1[2]#Delete element
6 print(list1)
7 list1+=[100,110]#Add a list to the original list
8 list1.append(200)#Add element at the end
It's still relatively simple here. There's nothing to say about the basic operation
1 a = ['a','b','c']
2 n = [1,2,3]
3 x = [a,n]
4 a[0] = 1
5 print(x)
6 print(x[0])
7 print(x[0][1])
This x is actually the combination of two lists, and x[0] is actually a. This is actually the same as the two-dimensional array access in c language.
1 b = a = [2,1]
2 a+=3
3 print(a,b)
4 a = a+[4,5]
5 print(a)
6 print(b)
The initial assignment is that b and a point to the same place. Adding an element at the position of a changes the value of this position, and b will change accordingly. If you use + =, it won't change.
Besides addition, there must be multiplication in the list:
Note that although the expression is multiplied by the corresponding multiple, each pointer is in the same position, and it is changed all the time.
1 a = [1,2,3,4] 2 b = a[1:3] #From 1 to 2 3 print (b) 4 b[0] = 100 5 print(b) 6 print(a) 7 print(a[::-1])#It is equivalent to printing in reverse. The last - 1 means the step size is - 1 8 print([1,2,3,4,5,6][1:5:2])#Steps from 1 to 4 are 2 9 print(a[:])#Is to traverse from beginning to end
II. Sorting of the list:
1 def selectsort(a): 2 n = n = len(a) 3 for i in range(n-1): 4 for j in range(i+1,n): 5 if a[j]<a[i]: 6 a[i],a[j] = a[j],a[i] 7 lst = [1,12,44,56,6,2] 8 selectsort(lst) 9 print(lst)
The above is the selective sorting of py. In fact, the idea is the same as that of the previous c language, but the representation is different. However, py provides many sorting functions in the standard library for us to use.
1 a = [5,7,6,3,4,1,2] 2 a.sort() 3 print(a) 4 a = [5,7,6,3,4,1,2] 5 b = sorted(a) 6 print(a) 7 print(b) 8 a.sort(reverse = True) #yes a Sort from large to small 9 print(a)
The explanation is actually the eighth line. This involves the following custom sorting. This reverse is actually a bit like a function. If the return value of this function is True, sort can be reversed. If it is a tuple or a list, the elements in the list are sorted according to the order of the elements in the list.
The sorting of custom comparison rules is often used for complex.
1 def mykey(x):
2 return x%10
3 a = [25,7,16,33,41,1,2]
4 a.sort(key = mykey)
5 print(a)
6 b = sorted("This is a test string from Andrew".split(),key = str.lower)
7 print(b)
For example, in the above code, I defined a function that will return the number of bits of a number. The function of key is to pass all elements in a into mykey as parameters to return the number of bits. Finally, this sort is sorted according to the size of the number of bits. b is to store the string as a list (the function of split), and then sort it case insensitive (that is, to turn all elements into lowercase).
1 students = [('john','A',15),('Mike','B',12),('Mike','C',18)]
2 students.sort(key = lambda x: x[2])#Sort by age
3 students.sort(key = lambda x :x[0])#Sort by name
Different keyword sorting will have different sorting effects. Later, let's talk about the function of lambda expression used above.
In fact, it is the writing method of a simple function. After passing 4 and 5 as parameters, it returns the value after the colon.
This is custom sorting. This is actually the problem of sorting- x[2] means to row from big to small according to age. But if it's not a list, it's a tuple. The result of using sort is a list.
Third, the use of map container (also used in c + +) and list filtering.
map(function,sequence) can be used to map one sequence to another
Return a delay evaluation object, which can be converted into list, tuple, set, etc
Here is an example:
1 def f(x):
2 print(x,end = "")
3 return x*x
4 a = map(f,[1,2,3])
5 print(list(a))
6 print(tuple(a))
7 a = list(map(lambda x:3*x,[2,3,4]))
8 print(a)
In my understanding, this map simply means that each element in the following list is passed into f (x) as a parameter, and the last returned value is stored and assigned to a container (list, tuple, etc.), but this value is not reflected in time. These values need to be stored in a container.
map can also be used for input, as follows:
1 x,y,z = map(int,input().split())
2 print(x,y,z)
Note that int is also a function. For example, int () can convert a data into int. After that is to convert the input x, y and z into elements in a list.
Map can also map multiple sequences into one sequence. This is to add up the corresponding values of each sequence and put them in the list of x.
Another cool thing behind is list filtering.
This filter is really convenient. You don't need to traverse and delete. Take a chestnut in the back:
1 def f(x):
2 return x%2 == 0
3 lst = tuple(filter(f,[1,2,3,4,5,6]))
4 print(lst)
4, List generating formula:
Let's give a bunch of examples to understand later:
Explain one by one later. In fact, to sum up, the function is placed in front of the for loop, and the judgment statement of for loop or if can be added after the for loop. The isinstance function is used to judge whether it is a certain type of number.
6, 2D list:
In fact, it's not much different from two-dimensional array, but you can take a look at two-dimensional list generation using list generation
This is quite different from c language. After all, there will be more functions in this. However, the definition of two-dimensional tuple will be a little troublesome. Tuple needs to be added in front of the expression, but it can also be understood. After all, if there is only one bracket, it will be too pale.
VII. Copy of the list:
If you only copy in this way, it is only a shallow copy. If you need a deep copy, you should copy it together with the pointer. If you copy in this way, a and b actually point to the same location. In this way, if you change the value of a, the value of b will also change. So here we need to introduce a library - copy.
The function of deepcopy is introduced to print together with the pointer.
8, List conversion:
Hoo ~, I'm so tired after writing a lot. The content of py is really miscellaneous, but it's still cool, hahaha!