如何编写一个名为 update 的函数来更新字典中的某些内容?

问题描述 投票:0回答:3

我想在 Python 中为

list
dictionary 创建一个名为 update 的函数,但我不知道如何做到这一点。

我尝试进行研究,但最常见的信息显示如何仅通过注释而不是功能进行更改。

我的字典在这里:

students = {
    "student1": {"name": "John", "age": 21, "live" :"UK"}
    "student2": {"name":"Steve", "age":25, "live": "USE"}
    "student3": {"name":"Tom", "age":32, "live": "France "}
    "student4": {"name":"Josh", "age":31, "live": "Spain"}
}

到目前为止,我使用此代码进行更新,但它根本没有运行

def update_students(students, student_id ,name, age ,live):
    students[student_id] = students.update({
        "name": name,
        "age": age,
        "live": live
    })
    return students, student_id ,name,age ,live

update_students(students,"student2","Alex", "32","japan ")
print(students)

但是没有任何行动...我可以纠正什么?

python function dictionary lis
3个回答
1
投票

对存储在

.update()
的字典调用
students[student_id]
,而不是将其分配给返回值(
.update()
始终返回
None
)。

代码:

students = {
 "student1": {"name": "John", "age": 21, "live" :"UK"},
 "student2": {"name":"Steve", "age":25, "live": "USE"},
 "student3": {"name":"Tom", "age":32, "live": "France "},
 "student4": {"name":"Josh", "age":31, "live": "Spain"}
}
 
def update_students(students, student_id ,name, age ,live):
    students[student_id].update({"name" :name,"age":age, "live": live})
    return students, student_id ,name,age ,live
    
update_students(students,"student2","Alex", "32","japan ")

print(students)

输出:

{'student1': {'name': 'John', 'age': 21, 'live': 'UK'}, 'student2': {'name': 'Alex', 'age': '32', 'live': 'japan '}, 'student3': {'name': 'Tom', 'age': 32, 'live': 'France '}, 'student4': {'name': 'Josh', 'age': 31, 'live': 'Spain'}}

0
投票

当您执行

dict.update
时,它会修改现有字典并向其添加键、值对或覆盖现有字典,并返回 None

>>> help(dict.update)
Help on method_descriptor:

update(...) unbound builtins.dict method
    D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
    If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
    If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
    In either case, this is followed by: for k in F:  D[k] = F[k]

在您正在执行的代码中

students[student_id]= students.update({"name" :name,"age":age, "live": live})
这表明您正在为键
student_id
分配新值,并且值是
students
字典上更新的输出。这是错误的。

但你想要的是键

student_id
更新/修改现有值(此处字典)

要更新现有字典,您要做的是

dictionary[key].update(some_another_dict)
并将新字典分配给现有键,请使用
=
操作,即
dictionary[key] = new_dictonary

下面是代码,它通过对现有字典键值使用

update
操作来解决您的问题

>>> students = {
...  "student1": {"name": "John", "age": 21, "live" :"UK"},
...  "student2": {"name":"Steve", "age":25, "live": "USE"},
...  "student3": {"name":"Tom", "age":32, "live": "France "},
...  "student4": {"name":"Josh", "age":31, "live": "Spain"}}
>>>
>>> def update_students(students, student_id ,name, age ,live):
...     students[student_id].update({"name" :name,"age":age, "live": live})
...     return students, student_id ,name,age ,live
...
>>> update_students(students,"student2","Alex", "32","japan ")
({'student1': {'name': 'John', 'age': 21, 'live': 'UK'}, 'student2': {'name': 'Alex', 'age': '32', 'live': 'japan '}, 'student3': {'name': 'Tom', 'age': 32, 'live': 'France '}, 'student4': {'name': 'Josh', 'age': 31, 'live': 'Spain'}}, 'student2', 'Alex', '32', 'japan ')
>>>

0
投票

首先,每个条目后的“students”中缺少逗号,其他内容在 update_students 函数中添加为注释

students = {
 "student1": {"name": "John", "age": 21, "live": "UK"},
 "student2": {"name": "Steve", "age":25, "live": "USE"},
 "student3": {"name": "Tom", "age":32, "live": "France "},
 "student4": {"name": "Josh", "age":31, "live": "Spain"}     
}
def update_students(students, student_id ,name, age ,live):
    # dict.update() returns None
    # Assuming you want to update values against student_id 
    # which itself is a dict, you can use below line to update that
    # Note that this is not accouting for student_id which doesn't exist
    # beforehand in students dict, so if you pass "student_5",
    # it will give KeyError
    students[student_id].update({"name": name,"age": age, "live": live})
    # No need for return statement, dictionary is mutable meaning you can 
    # update it in-place
update_students(students,"student2","Alex", "32", "japan")

print(students)

下面的函数也将负责添加新的student_id数据

def add_or_update_students(students, student_id, name, age, live):
    # check if the key already exists, if yes update
    # otherwise add that key in students dict
    if student_id in students:
       students[student_id].update({"name": name, "age": age, "live": live})
    else:
       students[student_id] = {"name": name, "age": age, "live": live}
© www.soinside.com 2019 - 2024. All rights reserved.