python - 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]]
解决方案
你可以试试这个: -
res = [[dict1[i] for i in j] for j in list1]
print(res)
输出:-
[[1, 2], [3], [1, 4]]
推荐阅读
- sql - 查询以显示今天在 postgres 中输入的记录(纪元时间)
- angularjs - $onInit 使用自己的作用域?
- android - 任何处理器都无法识别以下选项:'[kapt.kotlin.generated, room.incremental]'
- svn - 如何在 Windows 10 上将以“-”开头的文件添加到 svn
- php - 安全 URL 是使用 WordPress 返回 ERR_TOO_MANY_REDIRECTS
- python - 是否可以在不迭代\导入 Python 中的所有帧的情况下导入视频的第 #n 帧?
- google-cloud-platform - GCP LoadBalancer - 如何在运行状况检查失败时重定向
- python - Python:模块的延迟导入
- reactjs - Testcafe 忽略覆盖元素
- html - 如何仅向左侧的 div 添加 20px 边距(flex)