首页 > 解决方案 > Python从dict获取/转换嵌套列表中的值?

问题描述

我正在尝试从dict转换嵌套列表中的值:

list1 =  [["a","b"],["c"],["a","d"]]
dict1 = {"a":1,"b":2,"c":3,"d":4}

结果输出

new_list = [[1,2],[3],[1,4]]

标签: pythonlistdictionary

解决方案


你可以试试这个: -

res = [[dict1[i] for i in j] for j in list1]
print(res)

输出:-

[[1, 2], [3], [1, 4]]

推荐阅读