With respect to this question和this answer,我还有一些查询和一些详细信息。所以我在这里更新我的问题
现在数据是这样的
[OrderedDict([('caseId', 20), ('userId', 1), ('userName', 'user1'), ('emailStatus', 21), ('emailBody' , 'body')]),
OrderedDict([('caseId', 20), ('userId', 1), ('userName', 'user1'), ('emailStatus', 20), ('emailBody' , 'body')]),
OrderedDict([('caseId', 18), ('userId', 4), ('userName', 'user4'), ('emailStatus', 21), ('emailBody' , 'body')]),
OrderedDict([('caseId', 19), ('userId', 3), ('userName', 'user3'), ('emailStatus', 21), ('emailBody' , 'body')]),
OrderedDict([('caseId', 18), ('userId', 1), ('userName', 'user1'), ('emailStatus', 20), ('emailBody' , 'body')]),
OrderedDict([('caseId', 20), ('userId', 3), ('userName', 'user3'), ('emailStatus', 21), ('emailBody' , 'body')]),
OrderedDict([('caseId', 18), ('userId', 4), ('userName', 'user4'), ('emailStatus', 20), ('emailBody' , 'body')]),
OrderedDict([('caseId', 19), ('userId', 1), ('userName', 'user1'), ('emailStatus', 20), ('emailBody' , 'body')])]
我想得到一个嵌套列表的列表,类似这样的东西
[{
"caseId": "20",
"users": [
{
"userId": "1",
"userName" : "user1",
"emailStatus": [
{
"emailStatus" : "20",
"emailBody" : "body"
},
{
"emailStatus" : "21",
"emailBody" : "body"
}
]
},
{
"userId": "3",
"userName" : "user3",
"emailStatus": [
{
"emailStatus" : "21",
"emailBody" : "body"
}
]
}
]
},
{
"caseId": "19",
"users": [
{
"userId": "1",
"userName" : "user1",
"emailStatus": [
{
"emailStatus" : "20",
"emailBody" : "body"
}
]
},
{
"userId": "3",
"userName" : "user3",
"emailStatus": [
{
"emailStatus" : "21",
"emailBody" : "body"
}
]
}
]
},
{
"caseId": "18",
"users": [
{
"userId": "1",
"userName" : "user1",
"emailStatus": [
{
"emailStatus" : "20",
"emailBody" : "body"
}
]
},
{
"userId": "4",
"emailStatus": [
{
"emailStatus" : "20",
"emailBody" : "body"
},
{
"emailStatus" : "21",
"emailBody" : "body"
}
]
}
]
}
]
呈现这样的嵌套列表
我试过这样做
temp.setdefault(d["caseId"], {}).setdefault(str(d["userId"])+str(d["userName"]),[])
但是它将UserId
与UserName
连接起来,而不是生成新对象。。有什么猜测吗??你知道吗
不必将
userId
和userName
字段连接到一个字符串,您可以创建这两个字段中的tuple
,并将其用作temp
字典中的键。与status
和body
相同:或使用
defaultdict
:然后用
for (uid, uname), status in ...
再次解压这些元组相关问题 更多 >
编程相关推荐