Python 字典(Dictionary) setdefault()方法
2017-12-23 16:08
471 查看
描述
Python 字典 setdefault() 函数和get() 方法类似, 如果键不存在于字典中,将会添加键并将值设为默认值。
语法
setdefault()方法语法:dict.setdefault(key, default=None)
参数
key -- 查找的键值。default -- 键不存在时,设置的默认键值。
返回值
如果字典中包含有给定键,则返回该键对应的值,否则返回为该键设置的值。
实例
以下实例展示了 setdefault() 函数的使用方法:
实例(Python 2.0+)
#!/usr/bin/python#-*- coding: UTF-8 -*-
dict = {'runoob':
'菜鸟教程',
'google':
'Google 搜索'}
"Value : %s"
% dict.setdefault('runoob',
None)print
"Value : %s"
% dict.setdefault('Taobao',
'淘宝')
以上实例输出结果为:
Value : 菜鸟教程 Value : 淘宝
相关文章推荐
- Python 字典(Dictionary) setdefault()方法
- Python 字典(Dictionary) setdefault()方法
- Python 字典(Dictionary) setdefault()方法
- 在Python中操作字典之setdefault()方法的使用
- Python中字典的setdefault()方法教程
- Python 字典的setDefault()方法
- Python字典的setdefault()方法
- Python字典的setdefault()方法
- 在Python中操作字典之setdefault()方法的使用
- python中字典setdefault()方法理解。
- Python 字典(Dictionary) get()方法
- OC中NSDictionary(字典)、NSMutableDictionary(可变字典)、NSSet(集合)、NSMutableSet(可变集合)得常用方法
- Python 字典(Dictionary) get()方法
- python中字典的dic.setdefault()用法
- python字典(dictionary)中get和items、iteritems方法
- NSMutableDictionary 用set方法初始化的字典 里边的键值不可修改
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- 字典dictr的setdefault()方法如何使用
- python笔记--字典setdefault()应用
- Python中字典的get()方法以及defaultdict