Python字典(Dictionary)update()方法

原文连接:https://www.runoob.com/python/att-dictionary-update.html

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}

返回值:

该方法没有任何返回值。

猜你喜欢

转载自www.cnblogs.com/elitphil/p/11824304.html
今日推荐