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
连接在一起而不是制作新对象..任何猜测都可以吗?
答案 0 :(得分:2)
您可以创建两个userId
并将其用作userName
字典中的键,而不是将tuple
和temp
字段连接到一个字符串。 status
和body
相同:
temp = {}
for d in lst:
temp.setdefault(d["caseId"], {}).setdefault((d["userId"], d["userName"]), []).append((d["emailStatus"], d["emailBody"]))
print(temp)
# {18: {(1, 'user1'): [(20, 'body')], (4, 'user4'): [(21, 'body'), (20, 'body')]},
# 19: {(3, 'user3'): [(21, 'body')], (1, 'user1'): [(20, 'body')]},
# 20: {(3, 'user3'): [(21, 'body')], (1, 'user1'): [(21, 'body'), (20, 'body')]}}
或使用defaultdict
:
temp = defaultdict(lambda: defaultdict(list))
for d in lst:
temp[d["caseId"]][(d["userId"], d["userName"])].append((d["emailStatus"], d["emailBody"]))
然后使用for (uid, uname), status in ...
res = [{"caseId": case, "users": [{"userId": uid, "userName": uname, "emailStatus": [{"emailStatus": s, "emailBody": b}
for (s, b) in status]}
for (uid, uname), status in users.items()]}
for case, users in temp.items()]
print(res)
# [{'users': [{'userName': 'user1', 'userId': 1, 'emailStatus': [{'emailBody': 'body', 'emailStatus': 20}]}, {'userName': 'user4', 'userId': 4, 'emailStatus': [{'emailBody': 'body', 'emailStatus': 21}, {'emailBody': 'body', 'emailStatus': 20}]}], 'caseId': 18},
# {'users': [{'userName': 'user3', 'userId': 3, 'emailStatus': [{'emailBody': 'body', 'emailStatus': 21}]}, {'userName': 'user1', 'userId': 1, 'emailStatus': [{'emailBody': 'body', 'emailStatus': 20}]}], 'caseId': 19},
# {'users': [{'userName': 'user3', 'userId': 3, 'emailStatus': [{'emailBody': 'body', 'emailStatus': 21}]}, {'userName': 'user1', 'userId': 1, 'emailStatus': [{'emailBody': 'body', 'emailStatus': 21}, {'emailBody': 'body', 'emailStatus': 20}]}], 'caseId': 20}]