python-2.7 - 合并两个将所有参数从 list2 插入到 list1 的列表
问题描述
假设我有两个对象列表
List1 = [{"name" : "Mac", "age":24, "id" : 1},
{"name" : "Mona","age":22, "id" : 2}]
和
List2 = [{"type" : "human","country":"AUS"}]
如何将列表 2 中的所有元素附加到列表 1 的所有元素中,以便最终的 list1 看起来像
[{"name" : "Mac", "age":24, "id" : 1, "type" : "human","country":"AUS"},{"name" : "Mona", "age":22, "id" : 2, "type" : "human","country":"AUS"}]
目前正在循环并在列表上进行更新,但我想知道是否有更简单更好的方法来做到这一点
for person in List1:
person.update(List2)
print List1
解决方案
List1 = [{"name" : "Mac", "age":24, "id" : 1},
{"name" : "Mona","age":22, "id" : 2}]
List2 = [{"type" : "human","country":"AUS"}]
List1 = List1 + List2
这会起作用,我已经检查了 Python 2.7。希望这个答案有帮助。
推荐阅读
- ruby-on-rails - 如何在rails 5上的lib目录中的特定文件中使用模型
- r - 在选定的行中汇总
- python - 如何将文件中的数据存储到字典中?
- html - 当我用 包裹 div 时破坏了 flexbox 布局
- java - JPA 标准规范 - 多选与分组返回所有列
- database - 在 Pycharm 中的运行之间缓存数据库查询
- google-cloud-platform - gcloud run deploy 在本地工作,但不在 bitbucket 管道中
- php - 在 PHP 中按范围使用适当的公式
- javascript - 如何从 div 类 Cypress 中获取文本
- linux-kernel - CONFIG_RT_GROUP_SCHED 和 PREEMPT_RT 之间的区别?