python - 将 2 个相同大小的列表合并到一个 dicts Python 列表中
问题描述
我有两个相同大小的列表:
['/phoenix', '/scottsdale', '/tempe']
['Phoenix', 'Scottsdale', 'Tempe']
如何将它们组合到一个字典列表中:
[
{'slug': '/phoenix', 'title': 'Phoenix'},
{'slug': '/scottsdale', 'title': 'Scottsdale'},
{'slug': '/tempe', 'title': 'Tempe'}
]
解决方案
您可以使用:
l1 = ['/phoenix', '/scottsdale', '/tempe']
l2 = ['Phoenix', 'Scottsdale', 'Tempe']
print([{'slug': a, 'title': b} for (a, b) in zip(l1, l2)])
输出:
[{'slug': '/phoenix', 'title': 'Phoenix'},
{'slug': '/scottsdale', 'title': 'Scottsdale'},
{'slug': '/tempe', 'title': 'Tempe'}]
推荐阅读
- javascript - 需要 IE 填充程序的 Angular 5 和 IE11 问题
- vba - VBA Query returning nulls
- python - { "detail": "方法 \"GET\" 不允许。" }
- c++ - CMAKE Boost 单元测试框架在 Mac 上不起作用
- javascript - 如果所有输入字段不为空,则播放视频
- java - 在 iOS-Objective C 上签名的数据在 Java 中验证时总是返回 false
- visual-studio - Visual Studio 中的这个框叫什么,如何禁用它?
- php - SQL 从表中获取值
- android - FirebaseUI for Auth,自定义布局仅响应设置为 AuthMethodPickerLayout 的视图
- angularjs - 如何在angularJS中将范围属性显示为文本输入