Python 3.7 - basic list

Keywords: Python Fragment

1. Basic declaration and assignment of list

 1 #Declare an empty list variable = []  Or variable = list()
 2 var = [1]
 3 var2 = list()
 4 # Declare a list with data
 5 var = [1,2,3,'One','Two','Three']
 6 print(var)
 7 #Output results:[1, 2, 3, 'One', 'Two', 'Three']
 8 
 9 var2 = list(var)
10 print(var2)
11 #Output results:[1, 2, 3, 'One', 'Two', 'Three']

 


2. Get the value of the list through the index of the list

 

 1 #Declare a list
 2 lb = [1,2,3,'One','Two','Three']
 3 #From the first index of the list[0]Get the value of the list
 4 var = lb[0]
 5 print(var)
 6 #Output result: 1
 7 
 8 #From the last index of the list[-1]Get the value of the list, and the last second index is[-2]
 9 var2 =lb[-1]
10 print(var2)
11 #Output result: Three

 

 

 


3. Modify the value in the list through index

1 #Declare a list
2 lb = [1,2,3,'One','Two','Three']
3 #Modifying values in a list by index
4 lb[0] = 'I'
5 print(lb)
6 #Output results:['I', 2, 3, 'One', 'Two', 'Three']


4. Delete values in the list by index

1 #Declare a list
2 lb = [1,2,3,'One','Two','Three']
3 #Remove values from list by index
4 del lb[2]
5 print(lb)
6 #Output results:[1, 2, 'One', 'Two', 'Three']

 

5. Index cannot be used to add directly

...

6. Slice operation of list

 1 #Fragment operation of list
 2 #Declare a list
 3 lb = [1,2,3,'One','Two','Three']
 4 
 5 #variable[Start index:End index]   Get from start index to end index (excluding end index)
 6 var = lb[1:4]
 7 print(var)
 8 #Output results:[2, 3, 'One']
 9 
10 #variable[Start index:]   Intercept from start index position to last
11 var = lb[1:]
12 print(var)
13 #Output results:[2, 3, 'One', 'Two', 'Three']
14 
15 #variable[:End index]   From the beginning to the end (excluding the end index)
16 var = lb[:5]
17 print(var)
18 #Output results:[1, 2, 3, 'One', 'Two']
19 
20 #variable[:]                   Capture the entire list
21 var = lb[:]
22 print(var)
23 #Output results:[1, 2, 3, 'One', 'Two', 'Three']
24 
25 #variable[Start index:End index:Hop value]
26 var = lb[::2]
27 print(var)
28 #Output results:[1, 3, 'Two']

7. Fragment operation of list: add, delete, and modify

 1 #Fragment operation of list:Add, delete, change
 2 #Declare a list
 3 lb = [1,2,3,'One','Two','Three']
 4 
 5 #Partition assignment of list: (skip value cannot be set)
 6 #You can add, delete, modify and query the list.
 7 
 8 #1.add to the content
 9 #variable[Start index:End index] = List data (Note: the index in the partition is modified if the original list exists, and added if it does not exist)
10 lb[5:8] = ['I','you','he']
11 print(lb)
12 #Output results:[1, 2, 3, 'One', 'Two', 'I', 'you', 'he']
13 
14 #2.Delete content
15 #variable[Start index:End index] = []  (Deleted data from start index to end index, excluding end index position)
16 lb[0:5] =[]
17 print(lb)
18 #Output results:['I', 'you', 'he']
19 
20 #3.Modify content
21 #variable[Start index:End index] = Tabular data    (generally, the number of deletion and addition is the modification operation.)
22 lb[0:3] = ['Bo','passenger','garden']
23 print(lb)
24 #Output results:['Bo', 'passenger', 'garden']

 

8. Other operations of list

 

 1 #Other operations of the list:
 2 #Declare a list
 3 lb = [1,'One']
 4 #Multiply this list by four
 5 var = lb * 4
 6 print(var)
 7 #Output results:[1, 'One', 1, 'One', 1, 'One', 1, 'One']
 8 
 9 var2 =lb + ['you','I']
10 print(var2)
11 #Output results:[1, 'One', 'you', 'I']

 

 

 


9. In and not in the list

 1 #List in and not in
 2 #Declare a list
 3 lb = [1,2,3,'One','Two','Three']
 4 
 5 var ='Two' in lb
 6 print(var)
 7 #Output results: True
 8 
 9 var2='you' in lb
10 print(var2)
11 #Output results: False
12 
13 var3 = 'you' not in lb
14 print(var3)
15 #Output results: True

10. List common functions

 1 #List common functions:
 2 #Declare a list
 3 lb = [1,2,3,5,8,6,4]
 4 yz = ('One','Two','Three')
 5 
 6 #max()  Get the maximum value in the list or multiple parameters
 7 print(max(lb))
 8 #Output result:8
 9 
10 #min() Gets the minimum value in the list or in multiple parameters
11 print(min(lb))
12 #Output result:1
13 
14 #len()Get the length of container class data
15 print(len(lb))
16 #Output result:7
17 
18 #list() Declare a list or convert other container class data to a list
19 #Tuple group ('One','Two','Three')Lose as a list['One', 'Two', 'Three']
20 var = list(yz)
21 print(var)
22 #Output result:['One', 'Two', 'Three']


11. General for loop list

1 lb2 = [1,2,3,4,5,6,7,8,9,10]
2 oddlist = []
3 #ordinary for loop
4 for i in lb2:
5     #1 Secondary cycle oddlist[0:0] =[i],Here i To assign a value to a list, use brackets, i Is the value in the list
6     # 2 Secondary cycle oddlist[1:1] =[i],Because after the first cycle len(oddlist)The length of is 1
7     oddlist[len(oddlist):] = [i]
8 print(oddlist)
9 #Result:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 

12. Single cycle derivation with judgment

1 lb2 = [1,2,3,4,5,6,7,8,9,10]
2 #Single cycle derivation with judgment
3 var = [ i for i in lb2 if i % 2 == 1 ]
4 print(var)
5 #Result:[1, 3, 5, 7, 9]

 

13. Double cycle derivation

1 colors1 = ['red','powder','yellow']
2 colors2 = ['young','blue','purple']
3 #Double cycle derivation
4 result = [ i + j for i in colors1 for j in colors2]
5 print(result)
6 # Result:['Red and green', 'Red and blue', 'Red purple', 'Powder green', 'powder blue', 'Pink Purple', 'Huang Qing', 'Yellow blue', 'Yellow purple']


14. Double cycle derivation with judgment conditions

1 colors1 = ['red','powder','yellow']
2 colors2 = ['young','blue','purple']
3 #Double cycle derivation with judging conditions
4 result = [ i + j for i in colors1 for j in colors2 if i == 'red']
5 print(result)
6 # Result:['Red and green', 'Red and blue', 'Red purple']

 

15. List related functions (Methods)

 1 #List related functions (Methods):
 2 # Declare a list with data
 3 lb = [1,2,3,'One','Two','Three']
 4 lb2 = [1,2,3,1,2,2,5,6]
 5 
 6 #append() Add a data at the end of the list
 7 lb.append('Data added at the end')
 8 print(lb)
 9 #Output results:[1, 2, 3, 'One', 'Two', 'Three', 'Data added at the end']
10 
11 #insert() Add data before the specified index of the list
12 lb.insert(2,'Inserted data')
13 print(lb)
14 #Output results:[1, 2, 'Inserted data', 3, 'One', 'Two', 'Three', 'Data added at the end']
15 
16 #pop() Delete data at the specified index location of the list
17 lb.pop(1)
18 print(lb)
19 #Output results:[1, 'Inserted data', 3, 'One', 'Two', 'Three', 'Data added at the end']
20 
21 #remove() Delete the specified data in the list
22 lb.remove('Inserted data')
23 print(lb)
24 #Output results:[1, 3, 'One', 'Two', 'Three', 'Data added at the end']
25 
26 #copy() Copy list
27 var = lb.copy()
28 print(var)
29 #Output results:[1, 3, 'One', 'Two', 'Three', 'Data added at the end']
30 
31 
32 #count() Count the number of occurrences of the specified data in the list
33 print(lb2.count(2))
34 #Output: 3
35 
36 #extend() Merge 2 lists with 1 bit
37 lb.extend(lb2)
38 print(lb)
39 #Output results:[1, 3, 'One', 'Two', 'Three', 'Data added at the end', 1, 2, 3, 1, 2, 2, 5, 6]
40 
41 #reverse() List inversion
42 lb.reverse()
43 print(lb)
44 #Output results:[6, 5, 2, 2, 1, 3, 2, 1, 'Data added at the end', 'Three', 'Two', 'One', 3, 1]
45 
46 #sort() sort list
47 lb2.sort()
48 print(lb2)
49 #Output results:[1, 1, 2, 2, 2, 3, 5, 6]
50 
51 #index() Get the index of a value in the list
52 var2 = lb.index('Three')
53 print(var2)
54 #Output: 9
55 
56 #clear() clear list
57 lb.clear()
58 print(lb)
59 #Output results:[]

Posted by RunningUtes on Thu, 14 Nov 2019 11:23:50 -0800