一、连接两个dict

aaa = {'question_type': 'What is the overall condition of the given image?', 'multiple_choice_answer': 'flooded', 'answers': [{'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '1'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '2'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '3'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '4'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '5'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '6'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '7'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '8'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '9'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '10'}], 'image_id': '10165', 'answer_type': 'Condition_Recognition', 'question_id': '10165000'}
bbb = {'question_type': 'What is the overall condition of the given image?', 'multiple_choice_answer': 'flooded', 'answers': [{'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '1'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '2'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '3'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '4'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '5'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '6'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '7'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '8'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '9'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '10'}], 'image_id': '10166', 'answer_type': 'Condition_Recognition', 'question_id': '10166000'}
aaa.update(bbb)
print(aaa)

输出是:
{‘question_type’: ‘What is the overall condition of the given image?’, ‘multiple_choice_answer’: ‘flooded’, ‘answers’: [{‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘1’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘2’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘3’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘4’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘5’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘6’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘7’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘8’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘9’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘10’}], ‘image_id’: ‘10166’, ‘answer_type’: ‘Condition_Recognition’, ‘question_id’: ‘10166000’}
.
调用update函数时,两个dict并没有连接起来,奇怪了好久,发现是key重复导致原来的dict被新的dict覆盖了。

aaa = {'question_type1': 'What is the overall condition of the given image?', 'multiple_choice_answer1': 'flooded', 'answers1': [{'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '1'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '2'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '3'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '4'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '5'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '6'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '7'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '8'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '9'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '10'}], 'image_id1': '10165', 'answer_type1': 'Condition_Recognition', 'question_id1': '10165000'}
bbb = {'question_type': 'What is the overall condition of the given image?', 'multiple_choice_answer': 'flooded', 'answers': [{'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '1'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '2'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '3'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '4'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '5'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '6'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '7'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '8'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '9'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '10'}], 'image_id': '10166', 'answer_type': 'Condition_Recognition', 'question_id': '10166000'}
aaa.update(bbb)
print(aaa)

我把aaa中每个key后面多加了一个“1”,果然连起来了。

输出:{‘question_type1’: ‘What is the overall condition of the given image?’, ‘multiple_choice_answer1’: ‘flooded’, ‘answers1’: [{‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘1’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘2’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘3’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘4’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘5’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘6’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘7’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘8’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘9’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘10’}], ‘image_id1’: ‘10165’, ‘answer_type1’: ‘Condition_Recognition’, ‘question_id1’: ‘10165000’, ‘question_type’: ‘What is the overall condition of the given image?’, ‘multiple_choice_answer’: ‘flooded’, ‘answers’: [{‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘1’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘2’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘3’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘4’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘5’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘6’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘7’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘8’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘9’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘10’}], ‘image_id’: ‘10166’, ‘answer_type’: ‘Condition_Recognition’, ‘question_id’: ‘10166000’}
.

二、连接两个list

如果想要两个dict虽然keys相同,但要并列地连接起来,可以用list将他们括起再连接:
例子:

aaa = [{'question_type': 'What is the overall condition of the given image?', 'multiple_choice_answer': 'flooded', 'answers': [{'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '1'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '2'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '3'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '4'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '5'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '6'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '7'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '8'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '9'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '10'}], 'image_id': '10165', 'answer_type': 'Condition_Recognition', 'question_id': '10165000'}]
bbb = [{'question_type': 'What is the overall condition of the given image?', 'multiple_choice_answer': 'flooded', 'answers': [{'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '1'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '2'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '3'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '4'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '5'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '6'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '7'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '8'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '9'}, {'answer': 'flooded', 'answer_confidence': 'yes', 'answer_id': '10'}], 'image_id': '10166', 'answer_type': 'Condition_Recognition', 'question_id': '10166000'}]
print(aaa+bbb)

输出为:
[{‘question_type’: ‘What is the overall condition of the given image?’, ‘multiple_choice_answer’: ‘flooded’, ‘answers’: [{‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘1’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘2’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘3’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘4’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘5’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘6’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘7’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘8’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘9’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘10’}], ‘image_id’: ‘10165’, ‘answer_type’: ‘Condition_Recognition’, ‘question_id’: ‘10165000’}, {‘question_type’: ‘What is the overall condition of the given image?’, ‘multiple_choice_answer’: ‘flooded’, ‘answers’: [{‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘1’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘2’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘3’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘4’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘5’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘6’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘7’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘8’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘9’}, {‘answer’: ‘flooded’, ‘answer_confidence’: ‘yes’, ‘answer_id’: ‘10’}], ‘image_id’: ‘10166’, ‘answer_type’: ‘Condition_Recognition’, ‘question_id’: ‘10166000’}]

python/由于keys重复导致字典dict在调用update函数时被覆盖相关推荐

  1. Python中集合set和字典dict的用法区别

    Python中集合set和字典dict的用法区别 核心知识点 Python列表(list).元组(tuple).字典(dict)和集合(set)详解 Python set集合详解 1. Python创 ...

  2. 7.python基础之基础数据类型--字典dict

    1.字典: 字典是python中唯一 的映射类型,采用键值对(key-value)的形式存储数据. 字典是无序存储的.且key必须是可哈希的.可哈希表示key必须是不可变类型,如:数字.字符串.元组. ...

  3. python中字典和json的区别_详解python中的json和字典dict

    定义 python中,json和dict非常类似,都是key-value的形式,而且json.dict也可以非常方便的通过dumps.loads互转.既然都是key-value格式,为啥还需要进行格式 ...

  4. 在Python中如何对一个字典(dict)的值进行排序

    上代码 >>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}>>> sorted(xs.items(), key=lambda x: ...

  5. python可变参数的特点_可变参数**kwargs传入函数时的存储方式为( )_学小易找答案...

    [单选题]抗结核药联合用药的目的是: [单选题]女性,50岁,患耐青霉素的金葡菌性心内膜炎,青霉素试敏阴性,既往有慢性肾盂肾炎,应选用: [填空题]固态下原子(或分子)呈( )排列而形成的聚集状态,称 ...

  6. python中集合set,字典dict和列表list的区别以及用法

    python中set代表集合,list代表列表,dict代表字典 set和dict的区别在于,dict是存储key-value,每一个key都是唯一的,set相对于dict存储的是key,且key是唯 ...

  7. python 将字符串转换成字典dict

    JSON到字典转化: 输出dict类型 dictinfo = json.loads(json_str) 字典到JSON转化: 输出str类型 # 比如: info = {'name' : 'jay', ...

  8. python concat去除重复值语句_Python DataFrame使用drop_duplicates()函数去重(保留重复值,取重复值)...

    摘要 在进行数据分析时,我们经常需要对DataFrame去重,但有时候也会需要只保留重复值. 这里就简单的介绍一下对于DataFrame去重和取重复值的操作. 创建DataFrame 这里首先创建一个 ...

  9. python调用子函数时参数传递问题

    场景: python建立子函数,在主程序中进行调用. 问题描述 将全局变量传入子函数,子函数中对局部变量进行修改导致全局变量发生. 示例代码: import numpy as np def softm ...

最新文章

  1. mysql远程主机强迫关闭了一个现有连接_asp.net连接mysql出现了远程主机强迫关闭了一个现有的连接。!!!...
  2. IE6-IE9兼容性问题列表及解决办法_补遗漏之一:button的type默认值改变为submit了。
  3. 字符串中统计单词个数
  4. virtualbox安装时发生严重错误_Docker 安装(windows 10)
  5. 前端学习(3104):react-hello-虚拟dom和真实dom
  6. 全球网速最快的地方在哪里?中国固定宽带网速增长超美国!
  7. Hadoop大数据综合案例4-Hive数据分析
  8. elasticsearch 模板
  9. 从产品角度看人口政策和生育减少问题
  10. 美林公司的尽职调查应用程序被选入Deloitte Tohmatsu的并购咨询解决方案
  11. 私有云和服务器虚拟化的区别,私有云和服务器有什么区别
  12. 【usb】安卓usb网络共享(RNDIS)
  13. iOS11新特性,如何适配iOS11
  14. larval框架的联表查询
  15. Matlab GUI界面表格中数据导出到excel文件带标题
  16. Android应用禁止截屏
  17. 逻辑右移和算术右移有什么区别?
  18. 安装blockchain-explorer区块浏览器
  19. Unity3D之如何将包大小减少到极致
  20. 一个简单的加密解密程序

热门文章

  1. 就是这么简单,QQ被盗了可以这样找回来!
  2. 哈理工第六届程序设计大赛 G 逃脱(BFS)
  3. Android 平台camera相关梳理
  4. 【TED】How to get better at the things you care about?
  5. mbp 封神台靶场 一(笔记)
  6. 用RGBD投影激光雷达数据:depthimage_to_laserscan
  7. 网站关键字排名查询工具
  8. python语言是学什么的_Python语言应该学习什么?
  9. oracle系统资源,oracle占用系统资源很高sqlplus无响应
  10. 最大流问题与福特-富尔克森算法