Python列表append操作覆盖已有字典问题求助
Python列表中字典被覆盖问题解决
问题描述
我是Python新手,编写了一段程序,旨在接收员工输入信息并存入字典,再将该字典存入列表。但每次循环时,列表中已存储的字典都会被新字典覆盖。
原代码
total_properties_sold = 0 total_commission = 0 bonus_pay = 0 employees = [] #empty list to store dictonary of employees information employee = {} #empty dictonary to store the information of employees while True: number_of_employees = input("Enter number of employees who have worked this week: ") if number_of_employees.isdigit(): #validation check to ensure input is a digit number_of_employees = int(number_of_employees) #if input is digit, convert to int if number_of_employees >= 2 and number_of_employees <= 5: #validates the range of input break else: print("Input invalid... enter a digit\n") else: print("Input invalid... enter a digit\n") #for loop to enter the details of the number of employees for number in range(number_of_employees): name = input("Enter employee name: ") while True: #validation check to ensure input is a digit id = input("Enter employee id: ") if id.isdigit(): id = int(id) break else: print("Invalid data type... enter a number\n") while True: properties_sold = input("enter number of properties sold by employee: ") if properties_sold.isdigit(): properties_sold = int(properties_sold) break else: print("Invalid data type... enter a number") total_properties_sold += properties_sold #calculates the total properties sold this week employee_commission = properties_sold * 500 employee_commission = float(employee_commission) #converts commission into a float total_commission += employee_commission #calculates the total commission this week total_commission = float(total_commission) #converts total commission into float #add employee information into the employee dictonary employee["name"] = name employee["employee id"] = id employee["properties sold"] = properties_sold employee["commission"] = employee_commission employees.append(employee) #adding the employee dictonary into employees list print(employees)
预期输出
[{'name': 'employee1', 'employee id': 1, 'properties sold': 4, 'commission': 2000.0}, {'name': 'employee2', 'employee id': 2, 'properties sold': 3, 'commission': 1500.0}, {'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}]
实际输出
[{'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}, {'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}, {'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}]
问题原因
你只在循环外创建了一个employee字典对象,每次循环只是修改这个对象的键值对,列表中添加的都是同一个字典的引用。所有列表元素指向的都是同一个内存地址的字典,所以最后所有元素都会显示最后一次修改的内容。
解决方法
将employee = {}移到for循环内部,每次循环都创建一个新的字典对象,这样添加到列表中的每个字典都是独立的,不会互相覆盖。
修改后的代码
total_properties_sold = 0 total_commission = 0 bonus_pay = 0 employees = [] #empty list to store dictonary of employees information while True: number_of_employees = input("Enter number of employees who have worked this week: ") if number_of_employees.isdigit(): #validation check to ensure input is a digit number_of_employees = int(number_of_employees) #if input is digit, convert to int if number_of_employees >= 2 and number_of_employees <= 5: #validates the range of input break else: print("Input invalid... enter a digit\n") else: print("Input invalid... enter a digit\n") #for loop to enter the details of the number of employees for number in range(number_of_employees): employee = {} # 每次循环创建新的字典 name = input("Enter employee name: ") while True: #validation check to ensure input is a digit id = input("Enter employee id: ") if id.isdigit(): id = int(id) break else: print("Invalid data type... enter a number\n") while True: properties_sold = input("enter number of properties sold by employee: ") if properties_sold.isdigit(): properties_sold = int(properties_sold) break else: print("Invalid data type... enter a number") total_properties_sold += properties_sold #calculates the total properties sold this week employee_commission = properties_sold * 500 employee_commission = float(employee_commission) #converts commission into a float total_commission += employee_commission #calculates the total commission this week total_commission = float(total_commission) #converts total commission into float #add employee information into the employee dictonary employee["name"] = name employee["employee id"] = id employee["properties sold"] = properties_sold employee["commission"] = employee_commission employees.append(employee) #adding the employee dictonary into employees list print(employees)
内容的提问来源于stack exchange,提问作者Rohail
相关产品推荐
相关产品推荐

