In the previous article [software testing], 0 basic pure Xiaobai can also write python. Nanny level tutorial (III) shares the basic knowledge of Python. We will continue to share other contents this time.
catalogue
1, List
1. Set of ordered variable elements;
2. The element can be any data type;
3. Store multiple data;
4. Generally speaking, it is used to store the same type of data;
5. It is indicated by brackets [] and the data is separated by commas:
6. List = [data 1, data 2, data 3];
1a = [] 2b = [1,2,3] 3c = ["a","b","c"] 4d = [1,"b",[1,2]] 5print(type(a),a) 6print(type(b),b) 7print(type(c),c) 8print(type(d),d) 9# Output results 10<class 'list'> [] 11<class 'list'> [1, 2, 3] 12<class 'list'> ['a', 'b', 'c'] 13<class 'list'> [1, 'b', [1, 2]]
2, List element NEW
1.append(): add to the end
1ls = [1,2,3] 2ls[0] = "a" 3ls.append(4) 4print(ls) 5# Output results 6['a', 2, 3, 4]
2.insert(): add to the specified location
1ls = [1,2,3] 2ls.insert(2,"a") 3print(ls) 4# Output results 5[1, 2, 'a', 3]
3.append() adds the entire list
1ls = [1,2,3] 2ls.append([4,5,6]) 3print(ls) 4# Output results 5[1, 2, 3, [4, 5, 6]]
4. Add an element to the list
-
Method 1: splicing
1ls = [1,2,3] 2new_ls = [4,5,6] 3ls = ls + new_ls 4print(ls) 5# Output results 6[1, 2, 3, 4, 5, 6]
-
Method 2: extend()
1ls = [1,2,3] 2new_ls = [4,5,6] 3ls.extend(new_ls) 4print(ls) 5# Output results 6[1, 2, 3, 4, 5, 6]
3, List element modification
1. Assignment index
1ls = [1,2,3] 2ls[0] = "a" 3print(ls) 4# Output results 5['a', 2, 3]
2. Modify the members of the original list
Step 1: get the index of the first occurrence of data, list name. Index (data)
1py43 = ['Poetic dream','Happy Click','Microenterprise','peas','Yikouta','grapefruit'] 2index = py43.index("peas") 3print(index) 4# Output results 53
Step 2: replace the data on the location with another data, list variable name
1py43 = ['Poetic dream','Happy Click','Microenterprise','peas','Yikouta','grapefruit'] 2index = py43.index("peas") 3py43[index] = "Wood" 4print(py43) 5# Output results 6['Poetic dream', 'Happy Click', 'Microenterprise', 'Wood', 'Yikouta', 'grapefruit']
4, Query list elements
1. Index of the list, value: variable name [index]
1py43 = ['Poetic dream','Happy Click','Microenterprise','peas','Yikouta','grapefruit'] 2print(py43[0]) 3print(py43[-1]) 4print(py43[::-1]) 5# Output results 6 Poetic dream 7 grapefruit 8['grapefruit', 'Yikouta', 'peas', 'Microenterprise', 'Happy Click', 'Poetic dream']
2. Index
1ls = [0,1,2,3,4,5,6,7,8,9] 2print(ls[0]) 3print(ls[-1]) 4# Output results 50 69
3. Slice the list and return the index of the specified value:. index()
1ls = [0,1,2,3,4,5,6,7,8,9] 2print(ls[0:6]) 3print(ls[::-1]) 4# Output results 5[0, 1, 2, 3, 4, 5] 6[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
4. Slice
1ls = [1,2,3,4,3,2,1] 2num = ls.index(2) 3print(num) 4# Output results 51
5. Get the value of the specified number of occurrences
. index() the default index is 0, that is, find the location of the first value;
The second value is: for example, the first 2 (0,2) and the second 2 (1,2)
1ls = [1,2,3,4,3,2,1] 2num = ls.index(1,2) 3print(num) 4# Output results 56
6. Value of nested elements
1ls = ["Xinlan","179","18",["Bodybuilding","Swimming"],["Xiao Zhan","Wang Yibo"]] 2print(ls[3][1]) 3# Output results 4 Swimming
5, Other actions for the list
1. Statistics. count(): get the number of occurrences of a value in the list, and the list variable name. Count (value)
1ls = [1,2,3,4,3,2,1] 2num = ls.count(1) 3print(num) 4# Output results 52 6 7py43 = ['Poetic dream','Happy Click','Microenterprise','peas','Yikouta','grapefruit'] 8print(py43.count("peas")) 9# Output results 101
2. Get the list length len
1py43 = ['Poetic dream','Happy Click','Microenterprise','peas','Yikouta','grapefruit'] 2print(len(py43)) 3# Output results 46
3. Splicing
-
+
1a = [1,2,3]+[4,5,6] 2print(a) 3# Output results 4[1, 2, 3, 4, 5, 6]
-
. join splicing, new variable name = "splicer". Join (list), this method only supports all strings
1a = ['Hello', 'world!welcome', 'py43!'] 2b = "+".join(a) 3print(b) 4# Output results 5Hello+world!welcome+py43!
-
"". join() splice
1a = ['h', 'e', 'l', 'l', 'o', ',', 'w', 'o', 'r', 'l', 'd', '!'] 2a = "".join(a) 3print(a) 4# Output results 5hello,world!
4. String cutting. split: List variable name = string variable. split (cutter)
1a = "Hello,world!welcome,py43!" 2b = a.split(",") 3print(b) 4# Output results 5['Hello', 'world!welcome', 'py43!']
5. Ride*
1b = ["hello"]*2 2print(b) 3# Output results 4['hello', 'hello']
6, List sorting
1. List inversion and reverse order
-
Method 1: slice value in reverse order
1py43 = ['Poetic dream','Happy Click','Microenterprise','peas','Yikouta','grapefruit'] 2print(py43[::-1]) 3# Output results 4['grapefruit', 'Yikouta', 'peas', 'Microenterprise', 'Happy Click', 'Poetic dream']
-
Method 2: variable. reverse()
1py43 = ['Poetic dream','Happy Click','Microenterprise','peas','Yikouta','grapefruit'] 2py43.reverse() 3print(py43) 4# Output results 5['grapefruit', 'Yikouta', 'peas', 'Microenterprise', 'Happy Click', 'Poetic dream']
-
Reverse. reverse()
1ls = [1,2,3,4] 2ls.reverse() 3print(ls) 4# Output results 5[4, 3, 2, 1]
2. Ascending order: from small to large, variable. sort()
1ls = [1,21,33,22,12] 2ls.sort() 3print(ls) 4# Output results 5[1, 12, 21, 22, 33]
3. Positive alphabetic sorting: sorting can only be performed if there is an order to check
1ls = ["b","d","a","f","e","c"] 2ls.sort() 3print(ls) 4# Output results 5['a', 'b', 'c', 'd', 'e', 'f']
3. Reverse alphabetical order
1ls = ["b","d","a","f","e","c"] 2ls.sort(reverse=True) 3print(ls) 4# Output results 5['f', 'e', 'd', 'c', 'b', 'a']
5. Descending order
From large to small, the variable. sort(reverse=True)
1ls = [1,21,33,22,12] 2ls.sort(reverse=True) 3print(ls) 4# Output results 5[33, 22, 21, 12, 1]
6. Ranking of scores
1ls = [["lucky",90],["Little",98],["Hee hee",92]] 2ls.sort(reverse=True) 3print(ls) 4# Output results 5[['Little', 98], ['Hee hee', 92], ['lucky', 90]]
7. All methods of printing out the list
1print(dir(list)) 2# Output results 3['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
To be continued
Finally, I would like to thank everyone who carefully reads my article. Watching the rise and attention of fans all the way, reciprocity is always necessary. Although it is not very valuable, you can take it directly if you can use it:
These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse also accompanies tens of thousands of test engineers through the most difficult journey. I hope it can also help you!
In my QQ technology exchange group (technology exchange and resource sharing, advertising do not disturb)
You can take it away yourself, group number: free information in the 310357728 group is the essence of the author's more than 10 year test career.
If it helps you a little, your "praise" is the biggest driving force for Xiaobian's creation. See you in the next article
🌻 Haowen recommendation
Is there such a big difference between test and development salary in the same IT industry?