[英]how can i merge two nested list as dictionary?
I've been trying to understand how this problem works but i don't know how to merge two nested list into a dictionary.我一直试图了解这个问题是如何工作的,但我不知道如何将两个嵌套列表合并到字典中。
I need to create a function combine ().我需要创建一个 function 组合()。 If I have a nested list called info and i have another nested list called detail.
如果我有一个名为 info 的嵌套列表,并且我有另一个名为 detail 的嵌套列表。 i need to combine these two nested list as a dictionary and returns the dictionary.
我需要将这两个嵌套列表组合为字典并返回字典。 I don't quite actually know how to start my code.
我实际上并不知道如何开始我的代码。 As I don't actually know how to merge a nested list as dictionary.
因为我实际上不知道如何将嵌套列表合并为字典。
info = [["Kean", 36, "Comp Sci", "Dept 2"], ["Ethan", 24, "Engineer", "Dept 5"], ["Kin", 23, "Med Tech", "Dept 1"]]
detail = [("Kean", ['good', "very good", "pass"]), ("Ethan", ["fail", "good", "fail"])]
the output should look like this: output 应如下所示:
{"Kean": [["Kean", 36, "Comp Sci", "Dept 2"], ['good', "very good", "pass"]], 'Ethan': [["Ethan", 24, "Engineer", "Dept 5"], ["fail", "good", "fail"]]}
Using dict.setdefault
使用
dict.setdefault
Ex:前任:
result = {}
info = [["Kean", 36, "Comp Sci", "Dept 2"], ["Ethan", 24, "Engineer", "Dept 5"], ["Kin", 23, "Med Tech", "Dept 1"]]
detail = [("Kean", ['good', "very good", "pass"]), ("Ethan", ["fail", "good", "fail"])]
for key, *value in info + detail:
result.setdefault(key, []).append(value)
print(result)
Output: Output:
{'Ethan': [[24, 'Engineer', 'Dept 5'], [['fail', 'good', 'fail']]],
'Kean': [[36, 'Comp Sci', 'Dept 2'], [['good', 'very good', 'pass']]],
'Kin': [[23, 'Med Tech', 'Dept 1']]}
You can also use collections.defaultdict
您也可以使用
collections.defaultdict
Ex:前任:
from collections import defaultdict
result = defaultdict(list)
for key, *value in info + detail:
result[key].append(value)
Output: Output:
defaultdict(<class 'list'>,
{'Ethan': [[24, 'Engineer', 'Dept 5'], [['fail', 'good', 'fail']]],
'Kean': [[36, 'Comp Sci', 'Dept 2'],
[['good', 'very good', 'pass']]],
'Kin': [[23, 'Med Tech', 'Dept 1']]})
Assuming only matching names should be merged (as seen in the intended output of OP).假设只应合并匹配的名称(如 OP 的预期 output 所示)。 This would work.
这会奏效。
info = [["Kean", 36, "Comp Sci", "Dept 2"], ["Ethan", 24, "Engineer", "Dept 5"], ["Kin", 23, "Med Tech", "Dept 1"]]
detail = [("Kean", ['good', "very good", "pass"]), ("Ethan", ["fail", "good", "fail"])]
outdict = {}
for i in info:
nameInfo = i[0]
for j in detail:
nameDetail = j[0]
if nameDetail == nameInfo:
outdict[nameDetail] = [i, j[1]]
print(outdict)
Output: Output:
{'Kean': [['Kean', 36, 'Comp Sci', 'Dept 2'], ['good', 'very good', 'pass']], 'Ethan': [['Ethan', 24, 'Engineer', 'Dept 5'], ['fail', 'good', 'fail']]}
Sidenote;边注; this could be done a lot more efficiently in terms of processing time.
就处理时间而言,这可以更有效地完成。 But for a small sample size, as is asked in the original question, this works as intended.
但是对于小样本量,正如原始问题中所问的那样,这可以按预期工作。
You can do this like this:你可以这样做:
info = [["Kean", 36, "Comp Sci", "Dept 2"], ["Ethan", 24, "Engineer", "Dept 5"], ["Kin", 23, "Med Tech", "Dept 1"]]
detail = [("Kean", ['good', "very good", "pass"]), ("Ethan", ["fail", "good", "fail"])]
result={element[0]:[element] for element in info}
This will give you a dictionary with name as the key and info as value这将为您提供一个字典,其中名称为键,信息为值
for name in detail:
if name[0] in result.keys():
result[name[0]].append(name[1])
print(result)
What we did was to check for names in result's key and to append the detail in it's values.我们所做的是检查结果键中的名称以及 append 值中的详细信息。
{'Kean': [['Kean', 36, 'Comp Sci', 'Dept 2'], ['good', 'very good', 'pass']], 'Ethan': [['Ethan', 24, 'Engineer', 'Dept 5'], ['fail', 'good', 'fail']], 'Kin': [['Kin', 23, 'Med Tech', 'Dept 1']]}
I find it much easier!我觉得容易多了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.