Python 字典(Dictionary) setdefault()方法

描述

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

语法

setdefault()方法语法:

dict.setdefault(key, default=None)

参数

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

返回值

该方法没有任何返回值。

实例

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

#!/usr/bin/python

dict = {‘Name‘: ‘Zara‘, ‘Age‘: 7}dict_default = dict.setdefault(‘Sex‘, ‘Man‘)
print "Value : %s" %  dict.setdefault(‘Age‘, None)
print "Value : %s" %  dict.setdefault(‘Sex‘, ‘Man‘)

以上实例输出结果为:

Value : 7
Value : None

>>>dict_default>>>Man>>>dict =
{‘Name‘: ‘Zara‘, ‘Age‘: 7,‘Sex‘ :‘Man‘}
时间: 2024-08-05 02:22:58

Python 字典(Dictionary) setdefault()方法的相关文章

Python 字典(Dictionary) update()方法

Python 字典(Dictionary) update()方法 描述: Python 字典(Dictionary) update() 函数把字典dict2的键/值对更新到dict里. 语法: update()方法语法: dict.update(dict2) 参数: dict2 -- 添加到指定字典dict里的字典. 返回值: 该方法没有任何返回值. 实例: 以下实例展示了 update()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'A

Python 字典(Dictionary) get()方法

描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 key -- 字典中要查找的键. default -- 如果指定键的值不存在时,返回该默认值值. 返回值 返回指定键的值,如果值不在字典中返回默认值None. 实例 以下实例展示了 get()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'A

Python 字典(Dictionary) has_key()方法-判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false

描述 Python 字典(Dictionary) has_key() 函数用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false. 语法 has_key()方法语法: dict.has_key(key) 参数 key -- 要在字典中查找的键. 返回值 如果键在字典里返回true,否则返回false. 实例 以下实例展示了 has_key()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} pri

Python 字典(Dictionary) clear()方法-用于删除字典内所有元素

描述 Python 字典(Dictionary) clear() 函数用于删除字典内所有元素. 语法 clear()方法语法: dict.clear() 参数 NA. 返回值 该函数没有任何返回值. 实例 以下实例展示了 clear()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7}; print "Start Len : %d" % len(dict) dict.clear() print "End L

Python 字典(Dictionary) items()方法-以列表返回可遍历的(键, 值) 元组数组

描述 Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组. 语法 items()方法语法: dict.items() 参数 NA. 返回值 返回可遍历的(键, 值) 元组数组. 实例 以下实例展示了 items()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} print "Value : %s" % dict.items() 以上实例输出结果为: Value

Python 字典(Dictionary) items()方法

欢迎关注本人博客:云端筑梦师 描述 Python 字典 items() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回可遍历的(键, 值) 元组数组. 语法 Dict.items() 参数 NA 返回值 以列表形式返回可遍历的(键, 值) 元组数组. 实例 示例代码: D = {'Google': 'www.google.com', 'Runoob': 'www.runoob.com', 'taobao': 'www.taobao.com'} print("字典值 :

python 字典的setdefault方法

如果键在字典中,返回这个键所对应的值.如果键不在字典中,向字典 中插入这个键,并且以default为这个键的值,并返回 default.default的默认值为None >>> dict={}>>> dict['key']='a'>>> dict{'key': 'a'}>>> dict.setdefault('key', 'b')  # 键key存在,故返回简直a.'a'>>> dict{'key': 'a'}>

[python]Python 字典(Dictionary) update()方法

update() 函数把字典dict2的键/值对更新到dict里.如果后面的键有重复的会覆盖前面的语法dict.update(dict2) dict = {'Name': 'Zara', 'Age': 7}dict2 = {'Sex': 'female','Name':'zhangsan'}dict.update(dict2)print "Value : %s" % dict 结果: [email protected]:/home/tao# python Python 2.7.17 (

Python字典高级使用方法汇总

Python字典高级使用方法汇总 字典(dictionary)是python中的一种非常灵活和强大的数据结构,可以完成很多操作.本文总结了一些除了基本的初始化.赋值.取值之外的常用的字典使用方法. 字典基础参考: [1]:http://www.w3cschool.cc/python/python-dictionary.html [2]:http://www.111cn.net/phper/python/56355.htm [3]:http://skyfen.iteye.com/blog/5675