我的数据如下。
[
{
"id" : "123",
"type" : "process",
"entity" : "abc"
},
{
"id" : "456",
"type" : "product",
"entity" : "ab"
}
]
我虽然循环如下以获取ID和实体
for test in serializer.data:
qaResultUnique['id'] = test['id']
qaResultUnique['entity'] = test['entity']
uniqueList.append(qaResultUnique)
但是输出错误,因为两次都只能得到第二本字典。
[
{
"id" : "456",
"entity" : "ab"
},
{
"id" : "456",
"entity" : "ab"
}
]
我做错了,请帮忙。
您正在重用qaResultUnique
字典对象。每次在循环中创建一个新字典:
for test in serializer.data:
qaResultUnique = {}
qaResultUnique['id'] = test['id']
qaResultUnique['entity'] = test['entity']
uniqueList.append(qaResultUnique)
或更简洁地表达:
uniqueList = [{'id': test['id'], 'entity': test['entity']} for test in serializer.data]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句