The dictionary duplicate assignment in python, and the exception thrown in append to list

Today, I encountered a misuse of python dictionary. First code:

data = [{'id': '1', 'name': 'Administrators', 'role': 'admin', 'desc': 'system administrator', 'acl': None},
 {'id': '2', 'name': 'research and development', 'role': 'dev', 'desc': 'R & D personnel', 'acl': None},
 {'id': '3', 'name': 'test', 'role': 'qa', 'desc': 'Testers', 'acl': None},
 {'id': '4', 'name': 'project manager', 'role': 'pm', 'desc': 'project manager', 'acl': None},
 {'id': '5', 'name': 'product manager', 'role': 'po', 'desc': 'product manager', 'acl': None},
 {'id': '6', 'name': 'R & D Supervisor', 'role': 'td', 'desc': 'R & D Supervisor', 'acl': None},
 {'id': '7', 'name': 'Product supervisor', 'role': 'pd', 'desc': 'Product supervisor', 'acl': None},
 {'id': '8', 'name': 'Test Supervisor', 'role': 'qd', 'desc': 'Test Supervisor', 'acl': None},
 {'id': '9', 'name': 'top management', 'role': 'top', 'desc': 'top management', 'acl': None},
 {'id': '10', 'name': 'Other', 'role': 'others', 'desc': 'Other', 'acl': None},
 {'id': '11', 'name': 'guest', 'role': 'guest', 'desc': 'For guest', 'acl': None},
 {'id': '12', 'name': 'Limited user', 'role': 'limited', 'desc': 'Restricted user grouping(You can only edit content related to yourself)', 'acl': None}]
depts = []
tmp_dept = {}
tmp_role = {}
roles = []
for i in data:
    print (i)
    tmp_dept["name"] = i["name"]
    tmp_dept["id"] = i["id"]
    tmp_role["name"] = i["name"]
    tmp_role["role"] = i["role"]
    depts.append(tmp_dept)
    roles.append(tmp_role)
print(depts,roles)

The logic is quite simple, that is, the data in data is separated into two lists. In the separation process, use two dictionaries for temporary turnover.
But in the actual operation process, it did not get the expected results. The result is

[{'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}, {'name': 'Limited user', 'id': '12'}]
[{'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}, {'name': 'Limited user', 'role': 'limited'}]

The last data in date is repeated.
At this time, I realized that there should be a problem with appen's data. It is suspected that the dictionary assignment is abnormal. After the append statement, you can check the dictionary id and find that all the IDs are the same. That is to say, the dictionary inserted into the depts roles is the same, even though its value changes constantly during the iteration of the cycle date.
resolvent:
In each iteration loop, recreate the dictionary

The code is as follows

data = [{'id': '1', 'name': 'Administrators', 'role': 'admin', 'desc': 'system administrator', 'acl': None},
 {'id': '2', 'name': 'research and development', 'role': 'dev', 'desc': 'R & D personnel', 'acl': None},
 {'id': '3', 'name': 'test', 'role': 'qa', 'desc': 'Testers', 'acl': None},
 {'id': '4', 'name': 'project manager', 'role': 'pm', 'desc': 'project manager', 'acl': None},
 {'id': '5', 'name': 'product manager', 'role': 'po', 'desc': 'product manager', 'acl': None},
 {'id': '6', 'name': 'R & D Supervisor', 'role': 'td', 'desc': 'R & D Supervisor', 'acl': None},
 {'id': '7', 'name': 'Product supervisor', 'role': 'pd', 'desc': 'Product supervisor', 'acl': None},
 {'id': '8', 'name': 'Test Supervisor', 'role': 'qd', 'desc': 'Test Supervisor', 'acl': None},
 {'id': '9', 'name': 'top management', 'role': 'top', 'desc': 'top management', 'acl': None},
 {'id': '10', 'name': 'Other', 'role': 'others', 'desc': 'Other', 'acl': None},
 {'id': '11', 'name': 'guest', 'role': 'guest', 'desc': 'For guest', 'acl': None},
 {'id': '12', 'name': 'Limited user', 'role': 'limited', 'desc': 'Restricted user grouping(You can only edit content related to yourself)', 'acl': None}]
depts = []
roles = []
for i in data:
    tmp_dept = {}
    tmp_role = {}
    tmp_dept["name"] = i["name"]
    tmp_dept["id"] = i["id"]
    tmp_role["name"] = i["name"]
    tmp_role["role"] = i["role"]
    depts.append(tmp_dept)
    roles.append(tmp_role)
print(depts,roles)

Tags: Python supervisor

Posted on Thu, 19 Mar 2020 11:56:13 -0400 by bhawap