python - 在python问题中合并两个列表
问题描述
我在 python 中有两个列表
名称:
['Test 1', 'Test 2']
是真的:
[False, True]
我想将列表合并到一个列表中,结果是:
[{name: 'Test 1', isTrue: False}, {name: 'Test 2', isTrue: True}]
这是我尝试过的:
list = []
thisdict = {}
for name in names:
thisdict['name'] = name
thisdict['isTrue'] = True
list.append(thisdict)
问题是我不确定如何获得动态布尔值或如何更有效地做到这一点。
任何帮助将不胜感激。
解决方案
您可以使用列表理解和zip
:
>>> [dict(name=x, isTrue=y) for x, y in zip(names, isTrue)]
推荐阅读
- nativescript-vue - 网格布局中的非活动按钮 - Nativescript-vue
- python - Pandas - zfill 混合列中的仅数值
- jpa - JPA 合并覆盖延迟加载的属性
- php - 关闭弹出窗口后重定向到新页面
- ms-access - 如何在 Access 上删除多个选定的记录?
- mysql - 如何从 Laravel 创建这种类型的 JSON 数据?
- spring - @Transactional(rollbackFor = Exception.class) 和 @Transactional(propagation=Propagation.REQUIRED) 有什么区别
- javascript - 事件未使用函数绑定定义
- css - Internet Explorer 中 bootstrap 4 的 img-fluid 问题
- html - Z-Index 与 Slim Select 的可折叠 Div 中的溢出