[英]Append is overwriting existing dictionary in list
im pretty new to python and I have a python program which is supposed to take inputs and store them into a dictionary and store that dictionary into a list, but for some reason its overwriting the stored dictionary in the list with the new one at each loop.我对 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)
The expected output should be预期的 output 应该是
[{'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}]
but i get但我明白了
[{'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}]
This is because you assign employee = {}
at the beginning of the code, and it is never reassigned after that, so the code is always referring to the same object .这是因为您在代码的开头分配了
employee = {}
,并且此后永远不会重新分配,因此代码始终引用相同的 object 。
When you set employee["name"] = name
for the next employee, you're still referring to the same employee
object, and it affects the instance of employee
that is already in the list .当您为下一个员工设置
employee["name"] = name
时,您仍然指的是同一个employee
object,它会影响已经在列表中的employee
实例。
To fix this, assign employee
to a fresh dictionary at the top of the for loop:要解决此问题,请在 for 循环的顶部将
employee
分配给一个新字典:
for number in range(number_of_employees):
employee = {}
name = input("Enter employee name: ")
This reassigns the variable name employee
to a fresh value, and breaks the connection to the previous values.这会将变量名称
employee
重新分配给新值,并断开与先前值的连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.