Python 字典的setDefault()方法
2015-05-05 23:27
579 查看
setdefault(key[, default])
If key is in the dictionary, return its value. If not, insert key with a value
of default and returndefault. default defaults to None.
如果键在字典中,返回这个键所对应的值。如果键不在字典中,向字典 中插入这个键,并且以default为这个键的值,并返回 default。default的默认值为None
Python 2.5.4 (r254:67916, Dec 23 2008, 15:19:34) [MSC v.1400 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************
IDLE 1.2.4
>>> dict={}
>>> dict['key']='a'
>>> dict
{'key': 'a'}
>>> dict.setdefault('key', 'b') # 键key存在,故返回简直a.
'a'
>>> dict
{'key': 'a'}
>>> dict.setdefault('key0', 'b') # 键key0不存在,故插入此键,并以b为键值.
'b'
>>> dict
{'key0': 'b', 'key': 'a'}
>>>
If key is in the dictionary, return its value. If not, insert key with a value
of default and returndefault. default defaults to None.
如果键在字典中,返回这个键所对应的值。如果键不在字典中,向字典 中插入这个键,并且以default为这个键的值,并返回 default。default的默认值为None
Python 2.5.4 (r254:67916, Dec 23 2008, 15:19:34) [MSC v.1400 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************
IDLE 1.2.4
>>> dict={}
>>> dict['key']='a'
>>> dict
{'key': 'a'}
>>> dict.setdefault('key', 'b') # 键key存在,故返回简直a.
'a'
>>> dict
{'key': 'a'}
>>> dict.setdefault('key0', 'b') # 键key0不存在,故插入此键,并以b为键值.
'b'
>>> dict
{'key0': 'b', 'key': 'a'}
>>>
相关文章推荐
- python中字典setdefault()方法理解。
- Python 字典(Dictionary) setdefault()方法
- Python字典的setdefault()方法
- Python 字典(Dictionary) setdefault()方法
- Python字典的setdefault()方法
- Python 字典(Dictionary) setdefault()方法
- 在Python中操作字典之setdefault()方法的使用
- Python 字典(Dictionary) setdefault()方法
- 在Python中操作字典之setdefault()方法的使用
- Python中字典的setdefault()方法教程
- python中字典的dic.setdefault()用法
- Python 字典setdefault()
- 字典dictr的setdefault()方法如何使用
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- pyhon-字典的get()与setdefault()方法
- python中的setdefault()方法
- 在Python中使用defaultdict初始化字典以及应用方法
- P16 python setdefault()方法
- 决策树c4.5(python3字典各种方法,map(),集合set()等,python树节点)
- python中字典处理函数get和setdefault的区别