描述

Python 字典 update() 方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中。

用法与 Python dict() 函数相似。

语法

update() 方法语法:

D.update(key/value)

参数

  • key/value -- 用于更新字典的键/值对,此处可以表示键/值对的方法有很多,请看实例。

返回值

该方法没有任何返回值。

实例

以下实例展示了 update() 方法的使用方法:

# !/usr/bin/python3

D = {'one': 1, 'two': 2}

D.update({'three': 3, 'four': 4})  # 传一个字典
print(D) D.update(five=5, six=6) # 传关键字
print(D) D.update([('seven', 7), ('eight', 8)]) # 传一个包含一个或多个元祖的列表
print(D) D.update(zip(['eleven', 'twelve'], [11, 12])) # 传一个zip()函数
print(D) D.update(one=111, two=222) # 使用以上任意方法修改存在的键对应的值
print(D)

以上实例输出结果为:

{'one': 1, 'three': 3, 'two': 2, 'four': 4}
{'one': 1, 'four': 4, 'six': 6, 'two': 2, 'five': 5, 'three': 3}
{'one': 1, 'eight': 8, 'seven': 7, 'four': 4, 'six': 6, 'two': 2, 'five': 5, 'three': 3}
{'one': 1, 'eight': 8, 'seven': 7, 'four': 4, 'eleven': 11, 'six': 6, 'twelve': 12, 'two': 2, 'five': 5, 'three': 3}
{'four': 4, 'seven': 7, 'twelve': 12, 'six': 6, 'eleven': 11, 'three': 3, 'one': 111, 'eight': 8, 'two': 222, 'five': 5}

zip() 函数:http://www.cnblogs.com/wushuaishuai/p/7766470.html

最新文章

  1. css3伪类、伪元素选择器---nth-child()和nth-of-type()选择器的区别
  2. SJGestureUnlock快速集成手势解锁
  3. Oracle笔记1-数据库概念
  4. AX7: HOW TO USE TABLE METHOD EXTENSION CLASS
  5. nginx简单反向代理和负载均衡(ubuntu)
  6. redis学习-day1
  7. SIM900A访问HTTP的简单方法
  8. leetcode Invert Binary Tree python
  9. Qt 富文本处理
  10. bootstrap快速入门笔记(九)-响应式工具
  11. Centos 7.3下图文安装SQL Server
  12. Django-Views模块详解
  13. java~lambda表达式让查询更优雅
  14. http连接基础类,负责底层的http通信
  15. 前端基础:form表单提交
  16. zk创建集群
  17. Oracle null判断并替换空值
  18. ssh远程登录不上的处理
  19. 使用java进行 AES 加密 解密?
  20. 网页编程工具:EditPlus

热门文章

  1. Javascript 中“靠”的使用
  2. 关于C++ 中POD类型的解析
  3. android 下的网络图片加载
  4. 【Other】最近在研究的, Java/Springboot/RPC/JPA等
  5. SQL-查询排名
  6. [Bash] Create Aliases in .bash_profile for Common Bash Commands
  7. U盘去保护方法
  8. IIS 之 线程池最大线程数
  9. SettingsPLSQLDeveloper
  10. mysql利用yum安装指定数据存放路径