亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

Python 基礎教程

Python 流程控制

Python 函數(shù)

Python 數(shù)據(jù)類型

Python 文件操作

Python 對象和類

Python 日期和時間

Python 高級知識

Python 參考手冊

Python 字典 update() 使用方法及示例

Python 字典方法

update()方法向字典插入指定的項目。這個指定項目可以是字典或可迭代對象。

如果鍵不在字典中,則update()方法將元素添加到字典中。如果鍵在字典中,它將使用新值更新鍵。

update()的語法為:

dict.update([other])

update()參數(shù)

update()方法采用字典或鍵/值對(通常為元組)的可迭代對象  。

如果在不傳遞參數(shù)的情況下調(diào)用update(),則字典保持不變。

update()返回值 

update()方法使用字典對象或鍵/值對的可迭代對象中的元素更新字典。

它不返回任何值(返回None)。

示例1:update()如何在Python中工作?

d = {1: "one", 2: "three"}
d1 = {2: "two"}

# 更新key=2的值
d.update(d1)
print(d)

d1 = {3: "three"}

# 使用鍵3添加元素
d.update(d1)
print(d)

運行該程序時,輸出為:

{1: 'one', 2: 'two'}
{1: 'one', 2: 'two', 3: 'three'}

示例2:update()如何與Iterable一起使用?

d = {'x': 2}

d.update(y = 3, z = 0)
print(d)

運行該程序時,輸出為:

{'x': 2, 'y': 3, 'z': 0}

Python 字典方法