Python 字典 Dictionary setdefault()方法

Python 字典 DictionaryPython 字典 Dictionary

Python 字典 setdefault() 函数和 get()方法 类似, 如果键不存在于字典中,将会添加键并将值设为默认值。

 

语法

setdefault() 方法语法:

dict.setdefault(key, default=None)

 

参数

  • key -- 查找的键值。
  • default -- 键不存在时,设置的默认键值。

 

返回值

如果字典中包含有给定键,则返回该键对应的值,否则返回为该键设置的值。

 

实例

以下实例展示了 setdefault() 函数的使用方法:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
dict = {'codebaoku': '编程教程', 'google': 'Google 搜索'}
 
print "Value : %s" %  dict.setdefault('codebaoku', None)
print "Value : %s" %  dict.setdefault('Taobao', '淘宝')

以上实例输出结果为:

Value : 编程教程
Value : 淘宝

Python 字典 DictionaryPython 字典 Dictionary

Python 字典 Dictionary update()方法:Python 字典(Dictionary) update() 函数把字典dict2的键/值对更新到dict里。update()方法语法:dict.update(dict2)。返回值:该方法没有任何返回值。