Python字典(dictionary)update()函数把字典dict2的键/值对更新到dict里面。

意思就是把一个字典的键值对更新到另一个字典里。

实例:

dict = {'Name": 'Zara', 'Age':7}
dict2 ={ 'Sex': 'female' }
dict.update(dict2)
print "Value: %s" % dict

输出:

Value: {'Age': 7, 'Name': 'Zara', 'Sex': 'female' }

注意:有相同的键会直接替换成update的值:

a = {1: 2, 2: 2}
b = {1: 1, 3: 3}
b.update(a)
print (b)

输出:

{1: 2, 2: 2, 3: 3}

返回值:

该方法没有任何返回值。

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

update() 函数把字典dict2的键/值对更新到dict里.如果后面的键有重复的会覆盖前面的语法dict.update(dict2) dict = {'Name': 'Zara', 'Age': ...

Python 字典(Dictionary) update()方法
refer to: http://www.runoob.com/python/att-dictionary-update.html
Python 字典(Dictionary) clear()方法
Python 字典(Dictionary) clear()方法 描述 Python 字典(Dictionary) clear() 函数用于删除字典内所有元素.高佣联盟 www.cgewang.com ...
Python 字典(Dictionary) type()方法
Python 字典(Dictionary) type()方法 描述 Python 字典(Dictionary) type() 函数返回输入的变量类型,如果变量是字典就返回字典类型.高佣联盟 www.c ...
Python 字典(Dictionary) str()方法
Python 字典(Dictionary) str()方法 描述 Python 字典(Dictionary) str() 函数将值转化为适于人阅读的形式,以可打印的字符串表示.高佣联盟 www.cge ...
Python 字典(Dictionary) len()方法
Python 字典(Dictionary) len()方法 描述 Python 字典(Dictionary) len() 函数计算字典元素个数,即键的总数.高佣联盟 www.cgewang.com 语 ...
Python 字典(Dictionary) cmp()方法
Python 字典(Dictionary) cmp()方法 描述 Python 字典的 cmp() 函数用于比较两个字典元素.高佣联盟 www.cgewang.com 语法 cmp()方法语法: cm ...
Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
Python 字典(Dictionary) setdefault()方法
描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...