Consolidate the Python foundation
Three, list
In the related courses of "Introduction to Python", we learn the four major data structures of Python: lists, tuples, dictionaries and sets. Among them, the list is the most commonly used and the most flexible data structure. We also have a basic understanding of the concept and operation of the list. Today, we will review these most basic basic knowledge together, and then we will pass a case See how the list is used in actual applications?
(1) Review first
1、 The concept of a list
Python's list (list) is a flexible collection of ordered objects.
1、 Lists can contain any kind of objects, and can even be nested. A list can contain another list as one of the objects.
2、 The list contains mutable objects and supports real-time modification (modification in place).
3、 The list can be increased or decreased as needed.
4、 With the help of lists, we can almost create and process arbitrarily complex data information through scripts.
**The list is extremely important and the application is extremely common. **
2、 How to create a list
Putting the comma-separated objects in a pair of square brackets creates a list.
list1 = [1, 2, 3, 4, 5 ]
list2 = ['a', 'b', 'c', 'd']
list3 = [] #define an empty list
list4 = [1,] #Only one single item
PS: When a list is assigned to a variable name, it essentially creates a reference between the list object and the variable name.
3、 Access the value in the list
(1) Use the index to access the value in the list
list1 = ['a', 'b', 'c', 'd', 'e']
list1[1]
' b'
(2) Use slices to access the values in the list
list1 = ['a', 'b', 'c', 'd', 'e']
print (list1[1:3])
[' b', 'c',]
4、 update list
(1) Through the index, directly re-assign the value of the specified subscript.
(2) Use the append() method to append a new item to the end of the list.
(3) Use the del statement to delete the item with the specified subscript by index.
list1 = ['a', 'b', 'c', 'd', 'e']
list1[3] = 'f'
print(list1)
[' a', 'b', 'c', 'f', 'e']
list1.append('d')
print(list1)
[' a', 'b', 'c', 'f', 'e', 'd']
del list1[2]
print(list1)
[' a', 'b', 'f', 'e', 'd']
5、 List operation function
len(['a','b','c','d','e'])# Get the length of the list
5
[' a','b','c','d','e'] + ['f','g','h']# List combination (addition)
[' a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
[' a','b','c'] * 2#list repeat
[' a', 'b', 'c','a', 'b', 'c']
3 in [1,2,3] #in operator, to determine whether an element exists
True
for x in [1,2,3]#Iteration (traversal)
1 2 3
max([1,2,3,4,5])#Get the maximum value of the list
5
min([1,2,3,4,5])#Get the minimum value of the list
1
range(5)#Create a list of consecutive integers
[0,1,2,3,4]
PS: Both sides of the plus sign + must be lists. You cannot add a list and a string together.
6、 List operation method
(1), list.append(obj): add a new object at the end of the list.
(2), list.count(obj): Count the number of times an element appears in the list.
(3), list.index(obj): Find the index position of the first matching item of a certain value from the list.
(4), list.insert(index, obj): insert the object into the list (in front of the specified index position).
(5), list.pop(obj=list[-1]): Remove an element from the list (the last element by default), and return the value of the element.
(6), list.remove(obj): remove the first match of a value in the list.
(7), list.reverse(): Reverse arrangement of the original list.
(8), list.sort([func]): sort the original list.
(9), list.extend(seq): append another sequence value at the end of the list.
**Please note the situation when appending a sequence of strings: **
[1,2,3]. extend(‘xyz’)
[1,2,3, ’x’,’y’,’z’]
(Two), case application
The following table is a company’s employee evaluation form
Employee ID | Work Experience | Salary Grade | Attendance Rate | Overtime | Performance |
---|---|---|---|---|---|
1001 | 3 | 5 | 100 | 16 | 50000 |
1002 | 1 | 1 | 80 | 0 | 3000 |
1003 | 2 | 3 | 90 | 10 | 23000 |
1004 | 4 | 6 | 95 | 20 | 45000 |
1005 | 5 | 7 | 100 | 30 | 65000 |
In the process of evaluating performance and calculating bonuses, it is necessary to calculate the final performance bonus according to the weight of different assessment items. The calculation rules are as follows (assuming it is reasonable):
Employee bonus = performance * [(number of years of service/30) * 0.2 + (salary grade/12) * 0.3 + (attendance rate/100) * 0.3 + (number of overtime/120) * 0.2] /10
So, if we use Python to develop such a salary management system, when we encounter this problem, how should our Python program deal with this data and calculate the bonus of each employee the fastest?
Faced with such demands, as programmers, we should have the following thinking:
1、 As an application system, the program we wrote is not to calculate the bonus of these 5 people this month, but, according to the rules, can be used to calculate the bonus of each person every month.
2、 One person’s complete information is enough to calculate the person’s bonus amount, and everyone’s algorithm is the same. Then, you only need to loop iteratively according to the total number of (sales) employees in the database to calculate everyone’s bonus.
3、 The complete data information of each person constitutes a list, and different index values represent a single item of data, which can be used to calculate the person’s bonus.
# Step 1: Read the data of the database
user_info_table = []
user_info_table.append([1001,3,5,100,16,50000])
user_info_table.append([1002,1,1,80,0,3000])
user_info_table.append([1003,2,3,90,10,23000])
user_info_table.append([1004,4,6,95,20,45000])
user_info_table.append([1005,5,7,100,30,65000])
**# Step 2: A new list whose elements will consist of tuples of employee ID and bonus. **
user_reward_list = []
**# The third step: iterate the employee data table in a loop, and calculate the bonus of each employee. **
for x in user_info_table:
user_id = x[0]#Get employee ID
reward = x[5] * [(x[1] /30) * 0.2 + (x[2] /12) * 0.3 + (x[3]/100) * 0.3 + (x[4]/120) * 0.2] /10#Calculate bonus
user_reward = (user_id,reward)#Create a tuple
user_reward_list.appned(user_reward)#Add tuples to the list
for x in user_reward_list: print(x)
(1001, 2358.50)
(1002, 81.5)
(1003, 862.43)
(1004, 2226.15)
(1005, 3627.00)]
how about it?
A seemingly complex problem,
When we make good use of lists,
Data processing,
It becomes very simple!
Recommended Posts