简体   繁体   English

Python 从字典中获取/转换嵌套列表中的值?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM