用python将一个列表和一个字典匹配合并
列表是[['1号机器','01'],['2号机器','02']]字典是{"01":{"list":[{"ID":"a","Name":"Ua"},{"ID":"b","N...
列表是[['1号机器','01'],['2号机器','02']]
字典是{
"01":{"list":[{"ID":"a","Name":"Ua"},{"ID":"b","Name":"Ub"}]},
"02":{"list":[{"ID":"c","Name":"Uc"},{"ID":"d","Name":"Ud"}]}
}
字典匹配列表合成这种列表
[[['1号机器','01'],[['a','Ua'],['b','Ub']]],[['2号机器','02'],[['c','Uc'],['d','Ud']]]]
怎么合成
最好有python的代码
回答的好加分!! 展开
字典是{
"01":{"list":[{"ID":"a","Name":"Ua"},{"ID":"b","Name":"Ub"}]},
"02":{"list":[{"ID":"c","Name":"Uc"},{"ID":"d","Name":"Ud"}]}
}
字典匹配列表合成这种列表
[[['1号机器','01'],[['a','Ua'],['b','Ub']]],[['2号机器','02'],[['c','Uc'],['d','Ud']]]]
怎么合成
最好有python的代码
回答的好加分!! 展开
展开全部
l = [['1号机器', '01'], ['2号机器', '02']]
dic = '''{
"01":{"list":[{"ID":"a","Name":"Ua"},{"ID":"b","Name":"Ub"}]},
"02":{"list":[{"ID":"c","Name":"Uc"},{"ID":"d","Name":"Ud"}]}
}'''
dic = dic.replace(',', ',')
dic = eval(dic)
result = []
for i in l:
result.append([])
result[len(result) - 1].append(i)
for key, value in dic.items():
if i[1] == key:
item = []
for j in value['list']:
item.append([j['ID'], j['Name']])
result[len(result) - 1].append(item)
print(result)
追问
字典必须是'''{}'''这种格式吗?怎么弄成这种呢?
追答
是的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询