英文:
Python - Appending to a dictionary object is not working as expected
问题
Sure, here's the translated code part:
我可能没有正确引用事物,所以请容忍我。
我从一个API获取了一个用户列表。我得到的对象是一个字典(通过print(type)确认)。
这个字典具有以下内容:
> {'data': [{'username': 'user1', 'name': {'firstName': 'firstname1',
> 'lastName': 'lastname1'}, 'suspended': False, 'suspendedAt': None},
> {'username': 'user2', 'name': {'firstName': 'firstname2', 'lastName':
> 'lastname2'}, 'suspended': False, 'suspendedAt': None}]}
Please note that I've translated the code comments and the dictionary structure. If you need further assistance with the code or have any questions, feel free to ask.
英文:
I may not be referencing things properly so please bear with me.
I obtain a list of users from an API. The resulting object I have is a dictionary (confirmed with print(type)).
The dictionary has the following:
> {'data': [{'username': 'user1', 'name': {'firstName': 'firstname1',
> 'lastName': 'lastname1'}, 'suspended': False, 'suspendedAt': None},
> {'username': 'user2', 'name': {'firstName': 'firstname2', 'lastName':
> 'lastname2'}, 'suspended': False, 'suspendedAt': None}}]}
I need to add some entries to this dictionary, so I have compiled the following:
def whitelist_user_list(userlist):
# Add the admin accounts.
for user in vars.admin_whitelist:
user_dict = {'username': user, 'name': {'firstName': user, 'lastName': user}, 'suspended': False, 'suspendedAt': None}
user_dict = dict(user_dict)
userlist.update(user_dict)
return userlist
The code works, but, the entry I'm adding is added in an odd way, in that it's outside of the closing curly and square brackets and just added to the end as if it's not a part of the dictionary.
> {'data': [{'username': 'user1', 'name': {'firstName': 'firstname1',
> 'lastName': 'lastname1'}, 'suspended': False, 'suspendedAt': None},
> {'username': 'user2', 'name': {'firstName': 'firstname2', 'lastName':
> 'lastname2'}, 'suspended': False, 'suspendedAt': None}}], 'username':
> 'admin', 'name': {'firstName': 'admin', 'lastName': 'admin'},
> 'suspended': False, 'suspendedAt': None}
I've followed many different examples of how to add to an existing dictionary and some just outright fail, this is the only one that actually completes. The difficulty being I don't quite know exactly what search term to use and every example I've come across uses much simpler dictionaries as examples.
Please advise how to I properly add these entries to my dictionary?
答案1
得分: 2
看起来userlist是这样初始化的:
userlist = {'data': []}
即,该字典有一个单一的键('data'),其关联值为一个列表。
这意味着你可以这样做:
def whitelist_user_list(userlist):
# 添加管理员账户。
for user in vars.admin_whitelist:
user_dict = {'username': user, 'name': {'firstName': user, 'lastName': user}, 'suspended': False, 'suspendedAt': None}
userlist['data'].append(user_dict)
return userlist
英文:
It appears that userlist is initialised thus:
userlist = {'data': []}
i.e., the dictionary has a single key ('data') with a list as its associated value.
Which means that you could do this:
def whitelist_user_list(userlist):
# Add the admin accounts.
for user in vars.admin_whitelist:
user_dict = {'username': user, 'name': {'firstName': user, 'lastName': user}, 'suspended': False, 'suspendedAt': None}
userlist['data'].append(user_dict)
return userlist
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论