如何更改嵌套字典键的 int 值? (Python 3)
How can I change int value of nested dictionary key? (Python 3)
我必须为我的评估作品编写一个基于菜单的 'Employee Management' 程序。主菜单项之一是 'Employee Details',具有以下选项:
- 显示员工
- 添加新员工
- 删除员工
我已经完成了 1 和 2,但我在 3 中遇到了一个特定的功能。删除员工。添加员工后,它会存储在 employees_all
字典中:
{1: {'fullname': 'John Smith', 'phonenumber': '0400 000 000', 'designation': 'Technician'},
2: {'fullname': 'Jane Doe', 'phonenumber': '0411 111 111', 'designation': 'Manager'},
3: {'fullname': 'Jimmy Smithers', 'phonenumber': '0422 222 222', 'designation': 'Maintenance'}}
我在添加员工时故意分配了一个升序的int键。要删除员工,程序将员工 ID(键)作为输入,并从字典中删除该员工。
我想要的是,当(例如)员工 #2 被删除时,我希望员工 #3 的密钥变为 2,而员工 #4 的密钥变为 3等等,等等等等。我试图创建一些 for
循环,从大于已删除员工键值的字典键中减去 1,但我不断得到:unsupported operand type(s) for -: 'dict' and 'int'
每个菜单项都有自己的功能。对于这个,它是 remove_employee()
。请参阅下面的代码:
def remove_employee():
print("\n______________________________________________")
print("\n> MAIN MENU > EMPLOYEE DATA > REMOVE EMPLOYEE")
print("\n\nWhich employee would you like to delete?")
delnum = int(input("\n\nEmployee Number: "))
print("\n______________________________________________")
print("\n\nARE YOU SURE YOU WANT TO DELETE EMPLOYEE #",delnum,"?")
print("\n1. Yes\n2. No")
areyousure1 = input("\nEnter choice: ")
if areyousure1 == "1":
del(employees_all[delnum])
#### PLEASE HELP HERE ####
print("\n______________________________________________")
print("\nEMPLOYEE DATA SUCCESSFULLY DELETED")
time.sleep(1)
submenu1()
elif areyousure1 == "2":
print("\n______________________________________________")
print("\n--------- EMPLOYEE DATA NOT DELETED ----------")
time.sleep(1)
submenu1()
else:
print("\n______________________________________________")
print("\nINVALID INPUT! PLEASE ENTER A VALID NUMBER.")
time.sleep(1)
remove_employee()
您的数据结构不是最佳的。我认为最好的方法是使用数组并将 ID 字段添加到您的员工对象。
employees = [
{"id": 1, 'fullname': 'John Smith', 'phonenumber': '0400 000 000', 'designation': 'Technician'},
{"id": 2, 'fullname': 'Jane Doe', 'phonenumber': '0411 111 111', 'designation': 'Manager'},
{"id": 3, 'fullname': 'Jimmy Smithers', 'phonenumber': '0422 222 222', 'designation': 'Maintenance'}
]
def delete_employee(id):
employees = [emp for emp in employees if emp.id != id]
# the new employees list is without the id you have removed
但是,如果您出于任何原因想要保留 pthis 数据结构,您可以使用此代码:
employees = {1: ...}
def delete_employee(id):
employees = {emp_id, employees[id] for emp_id in employees if emp_id != id}
# the new employees list without unwanted employee
我强烈建议您为此目的使用列表。
employees_all =
[{'fullname': 'John Smith', 'phonenumber': '0400 000 000', 'designation': 'Technician'},
{'fullname': 'Jane Doe', 'phonenumber': '0411 111 111', 'designation': 'Manager'},
{'fullname': 'Jimmy Smithers', 'phonenumber': '0422 222 222', 'designation': 'Maintenance'}]
这不仅负责索引,在删除 del
:
的条目后索引也会移动
del employees_all[index]
如果我理解正确,您需要将 delnum
之后的所有 employees_all
键都设置为 -1。请参阅 this answer 了解如何更改字典键。你的答案是:
for i in range(delnum+1,len(employees_all)):
employees_all[i-1]=employees_all.pop(i)
我必须为我的评估作品编写一个基于菜单的 'Employee Management' 程序。主菜单项之一是 'Employee Details',具有以下选项:
- 显示员工
- 添加新员工
- 删除员工
我已经完成了 1 和 2,但我在 3 中遇到了一个特定的功能。删除员工。添加员工后,它会存储在 employees_all
字典中:
{1: {'fullname': 'John Smith', 'phonenumber': '0400 000 000', 'designation': 'Technician'},
2: {'fullname': 'Jane Doe', 'phonenumber': '0411 111 111', 'designation': 'Manager'},
3: {'fullname': 'Jimmy Smithers', 'phonenumber': '0422 222 222', 'designation': 'Maintenance'}}
我在添加员工时故意分配了一个升序的int键。要删除员工,程序将员工 ID(键)作为输入,并从字典中删除该员工。
我想要的是,当(例如)员工 #2 被删除时,我希望员工 #3 的密钥变为 2,而员工 #4 的密钥变为 3等等,等等等等。我试图创建一些 for
循环,从大于已删除员工键值的字典键中减去 1,但我不断得到:unsupported operand type(s) for -: 'dict' and 'int'
每个菜单项都有自己的功能。对于这个,它是 remove_employee()
。请参阅下面的代码:
def remove_employee():
print("\n______________________________________________")
print("\n> MAIN MENU > EMPLOYEE DATA > REMOVE EMPLOYEE")
print("\n\nWhich employee would you like to delete?")
delnum = int(input("\n\nEmployee Number: "))
print("\n______________________________________________")
print("\n\nARE YOU SURE YOU WANT TO DELETE EMPLOYEE #",delnum,"?")
print("\n1. Yes\n2. No")
areyousure1 = input("\nEnter choice: ")
if areyousure1 == "1":
del(employees_all[delnum])
#### PLEASE HELP HERE ####
print("\n______________________________________________")
print("\nEMPLOYEE DATA SUCCESSFULLY DELETED")
time.sleep(1)
submenu1()
elif areyousure1 == "2":
print("\n______________________________________________")
print("\n--------- EMPLOYEE DATA NOT DELETED ----------")
time.sleep(1)
submenu1()
else:
print("\n______________________________________________")
print("\nINVALID INPUT! PLEASE ENTER A VALID NUMBER.")
time.sleep(1)
remove_employee()
您的数据结构不是最佳的。我认为最好的方法是使用数组并将 ID 字段添加到您的员工对象。
employees = [
{"id": 1, 'fullname': 'John Smith', 'phonenumber': '0400 000 000', 'designation': 'Technician'},
{"id": 2, 'fullname': 'Jane Doe', 'phonenumber': '0411 111 111', 'designation': 'Manager'},
{"id": 3, 'fullname': 'Jimmy Smithers', 'phonenumber': '0422 222 222', 'designation': 'Maintenance'}
]
def delete_employee(id):
employees = [emp for emp in employees if emp.id != id]
# the new employees list is without the id you have removed
但是,如果您出于任何原因想要保留 pthis 数据结构,您可以使用此代码:
employees = {1: ...}
def delete_employee(id):
employees = {emp_id, employees[id] for emp_id in employees if emp_id != id}
# the new employees list without unwanted employee
我强烈建议您为此目的使用列表。
employees_all =
[{'fullname': 'John Smith', 'phonenumber': '0400 000 000', 'designation': 'Technician'},
{'fullname': 'Jane Doe', 'phonenumber': '0411 111 111', 'designation': 'Manager'},
{'fullname': 'Jimmy Smithers', 'phonenumber': '0422 222 222', 'designation': 'Maintenance'}]
这不仅负责索引,在删除 del
:
del employees_all[index]
如果我理解正确,您需要将 delnum
之后的所有 employees_all
键都设置为 -1。请参阅 this answer 了解如何更改字典键。你的答案是:
for i in range(delnum+1,len(employees_all)):
employees_all[i-1]=employees_all.pop(i)