简体   繁体   English

如何在 python 的字典中查找和更新信息?

[英]How can I find and update info in a dictionary in python?

Hi I am new to python and I have a simple question, I have a list consisting of some user info and I want to know how can I write a program to find and update some of that info.嗨,我是 python 的新手,我有一个简单的问题,我有一个包含一些用户信息的列表,我想知道如何编写程序来查找和更新其中的一些信息。

user_list = [
    {'name': 'Alizom_12',
     'gender': 'f',
     'age': 34,
     'active_day': 170},
    {'name': 'Xzt4f',
     'gender': None,
     'age': None,
     'active_day': 1152},
    {'name': 'TomZ',
     'gender': 'm',
     'age': 24,
     'active_day': 15},
    {'name': 'Zxd975',
     'gender': None,
     'age': 44,
     'active_day': 752},
] 

what I did for finding a user is the following but I want to change it to display the info of a user rather than just printing the user exists:我为查找用户所做的工作如下,但我想更改它以显示用户的信息,而不仅仅是打印用户存在:

def find_user(user_name):
    for items in user_list:
        if items['name'] == user_name:
            return f'{user_name} exists. '
    return f'{user_name}  does not exists'

Also for updating the user info:还用于更新用户信息:

def update_user_info(user_name, **kw):
    user_list.update({'name': name, 'gender': gender, 'age': age, 'active_day': active_day})

    
    return user_list
print(find_user('Alizom_12'))
update_user_info('Alizom_12', **{'age': 29})
print(find_user('Alizom_12'))

Just return items as you need the user info in find_user, and update the specific dict values in the list for update_user_info.只需在 find_user 中返回您需要的用户信息的项目,并更新 update_user_info 列表中的特定 dict 值。

def find_user(user_name):
    for items in user_list:
        if items['name'] == user_name:
            return items
    return f'{user_name}  does not exists'

def update_user_info(user_name, **kw):
    for items in user_list:
        if items['name'] == user_name:
            for k in kw:
                items.update({k: kw[k]})

print(find_user('Alizom_12'))
update_user_info('Alizom_12', **{'age': 29})
print(find_user('Alizom_12'))

Output: Output:

{'name': 'Alizom_12', 'gender': 'f', 'age': 34, 'active_day': 170}
{'name': 'Alizom_12', 'gender': 'f', 'age': 29, 'active_day': 170}

If you want to display the user info, just change your return如果您想显示用户信息,只需更改您的return

return f'{items['name']:20} {items['gender']} {items['age']:3d} {items['active_day']:5d}'

Same for updating: find the item and update it.更新也一样:找到项目并update它。

As a side note, these json_like structures are frequent but they are not the most efficient way to handle data.附带说明一下,这些 json_like 结构很常见,但它们并不是处理数据的最有效方式。 A nested dict for example, or a dict of a custom class instances, are far easier to handle.例如,嵌套dict或自定义 class 实例的dict更容易处理。

user_dict = {'Alizom_12':{'gender': 'f','age': 34,'active_day': 170},
             'Xzt4f':{'gender': None,'age': None,'active_day': 1152}
            }
#check user
user_name in user_dict.keys()
#update user
user_dict[user_name].update(**kw)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM