将有序字典的列表转换为具有多个值的嵌套列表 [英] Convert list of ordered dict to nested lists with multiple values
问题描述
With respect to this question and this answer , I've some more queries with some detailed information. So here I'm updating my question
现在的数据是这样的
[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')])]
我想要一个嵌套列表的列表,像这样;
I want to get a list of nested lists, something like this;
[{
"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"
}
]
}
]
}
]
呈现这样的嵌套列表;
presenting a nested list like this;
我试图做这样的事情
temp.setdefault(d["caseId"], {}).setdefault(str(d["userId"])+str(d["userName"]),[])
但是它将UserId
与UserName
串联在一起,而不是创建新对象.请进行任何猜测??
But it is concatenating UserId
with UserName
instead of making new object.. Any guesses please??
推荐答案
您可以创建两个字段的tuple
并将其用作键,而不是将userId
和userName
字段串联为一个字符串. temp
字典. status
和body
相同:
Instead of concatenating the userId
and userName
fields to one string, you can create a tuple
of the two and use that as key in the temp
dictionary. Same for status
and 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}]
这篇关于将有序字典的列表转换为具有多个值的嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!