當前位置:成語大全網 - 新華字典 - 用python將壹個列表和壹個字典匹配合並

用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)