簡體   English   中英

Append 正在覆蓋列表中的現有字典

[英]Append is overwriting existing dictionary in list

我對 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)

預期的 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}]

但我明白了

[{'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 = {} ,並且此后永遠不會重新分配,因此代碼始終引用相同的 object

當您為下一個員工設置employee["name"] = name時,您仍然指的是同一個employee object,它會影響已經在列表中employee實例。

要解決此問題,請在 for 循環的頂部將employee分配給一個新字典:

for number in range(number_of_employees):
    employee = {}
    name = input("Enter employee name: ")

這會將變量名稱employee重新分配給新值,並斷開與先前值的連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM