python中字典重复赋值,append到list中引发的异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中字典重复赋值,append到list中引发的异常相关的知识,希望对你有一定的参考价值。

今天遇到了一个关于python 字典的误用。先上代码:

data = [{‘id‘: ‘1‘, ‘name‘: ‘管理员‘, ‘role‘: ‘admin‘, ‘desc‘: ‘系统管理员‘, ‘acl‘: None},
 {‘id‘: ‘2‘, ‘name‘: ‘研发‘, ‘role‘: ‘dev‘, ‘desc‘: ‘研发人员‘, ‘acl‘: None},
 {‘id‘: ‘3‘, ‘name‘: ‘测试‘, ‘role‘: ‘qa‘, ‘desc‘: ‘测试人员‘, ‘acl‘: None},
 {‘id‘: ‘4‘, ‘name‘: ‘项目经理‘, ‘role‘: ‘pm‘, ‘desc‘: ‘项目经理‘, ‘acl‘: None},
 {‘id‘: ‘5‘, ‘name‘: ‘产品经理‘, ‘role‘: ‘po‘, ‘desc‘: ‘产品经理‘, ‘acl‘: None},
 {‘id‘: ‘6‘, ‘name‘: ‘研发主管‘, ‘role‘: ‘td‘, ‘desc‘: ‘研发主管‘, ‘acl‘: None},
 {‘id‘: ‘7‘, ‘name‘: ‘产品主管‘, ‘role‘: ‘pd‘, ‘desc‘: ‘产品主管‘, ‘acl‘: None},
 {‘id‘: ‘8‘, ‘name‘: ‘测试主管‘, ‘role‘: ‘qd‘, ‘desc‘: ‘测试主管‘, ‘acl‘: None},
 {‘id‘: ‘9‘, ‘name‘: ‘高层管理‘, ‘role‘: ‘top‘, ‘desc‘: ‘高层管理‘, ‘acl‘: None},
 {‘id‘: ‘10‘, ‘name‘: ‘其他‘, ‘role‘: ‘others‘, ‘desc‘: ‘其他‘, ‘acl‘: None},
 {‘id‘: ‘11‘, ‘name‘: ‘guest‘, ‘role‘: ‘guest‘, ‘desc‘: ‘For guest‘, ‘acl‘: None},
 {‘id‘: ‘12‘, ‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘, ‘desc‘: ‘受限用户分组(只能编辑与自己相关的内容)‘, ‘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)

逻辑挺简单的,就是吧data中的数据分离成两个列表。在分离过程中,用两个字典临时周转一下。
但是在实际运行过程中,却没有得到期望的结果。得到的结果是

[{‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}, {‘name‘: ‘受限用户‘, ‘id‘: ‘12‘}]
[{‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}, {‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘}]

date中最后一条数据重复出现了。
这时意识到,应该是appen的数据有问题。怀疑是字典赋值异常。于是在append语句后查看字典id,结果发现所有的id是相同的。也就是说,插入到depts roles中的字典是同一个,即便是在迭代循环date的过程中,他的value不停地变化。
解决方法:
在每次迭代循环里,重新创建字典

代码如下

data = [{‘id‘: ‘1‘, ‘name‘: ‘管理员‘, ‘role‘: ‘admin‘, ‘desc‘: ‘系统管理员‘, ‘acl‘: None},
 {‘id‘: ‘2‘, ‘name‘: ‘研发‘, ‘role‘: ‘dev‘, ‘desc‘: ‘研发人员‘, ‘acl‘: None},
 {‘id‘: ‘3‘, ‘name‘: ‘测试‘, ‘role‘: ‘qa‘, ‘desc‘: ‘测试人员‘, ‘acl‘: None},
 {‘id‘: ‘4‘, ‘name‘: ‘项目经理‘, ‘role‘: ‘pm‘, ‘desc‘: ‘项目经理‘, ‘acl‘: None},
 {‘id‘: ‘5‘, ‘name‘: ‘产品经理‘, ‘role‘: ‘po‘, ‘desc‘: ‘产品经理‘, ‘acl‘: None},
 {‘id‘: ‘6‘, ‘name‘: ‘研发主管‘, ‘role‘: ‘td‘, ‘desc‘: ‘研发主管‘, ‘acl‘: None},
 {‘id‘: ‘7‘, ‘name‘: ‘产品主管‘, ‘role‘: ‘pd‘, ‘desc‘: ‘产品主管‘, ‘acl‘: None},
 {‘id‘: ‘8‘, ‘name‘: ‘测试主管‘, ‘role‘: ‘qd‘, ‘desc‘: ‘测试主管‘, ‘acl‘: None},
 {‘id‘: ‘9‘, ‘name‘: ‘高层管理‘, ‘role‘: ‘top‘, ‘desc‘: ‘高层管理‘, ‘acl‘: None},
 {‘id‘: ‘10‘, ‘name‘: ‘其他‘, ‘role‘: ‘others‘, ‘desc‘: ‘其他‘, ‘acl‘: None},
 {‘id‘: ‘11‘, ‘name‘: ‘guest‘, ‘role‘: ‘guest‘, ‘desc‘: ‘For guest‘, ‘acl‘: None},
 {‘id‘: ‘12‘, ‘name‘: ‘受限用户‘, ‘role‘: ‘limited‘, ‘desc‘: ‘受限用户分组(只能编辑与自己相关的内容)‘, ‘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)

以上是关于python中字典重复赋值,append到list中引发的异常的主要内容,如果未能解决你的问题,请参考以下文章

python内置数据类型列表list和字典dict的性能

python 18:字典的赋值

Python中List的append引用赋值问题处理

Python 字典中一键对应多个值

把1到100的数字拆分成字典中的两个键值对 python

python之路——列表,集合,字典