[英]Python Get/convert values in nested list from dict?
I'm trying to convert values in nested list from dict:我正在尝试从dict转换嵌套列表中的值:
list1 = [["a","b"],["c"],["a","d"]]
dict1 = {"a":1,"b":2,"c":3,"d":4}
resultant_output结果输出
new_list = [[1,2],[3],[1,4]]
You can do it with list
comprehension, assuming the values of list1
exist in dict1
假设
list1
的值存在于dict1
中,您可以使用list
理解来做到这一点
new_list = [[dict1[v] for v in k] for k in list1]
You can try this:-你可以试试这个: -
res = [[dict1[i] for i in j] for j in list1]
print(res)
Output:- Output:-
[[1, 2], [3], [1, 4]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.