python 列表中的Update()函數

來源:互聯網
上載者:User

標籤:函數   說明   nic   多個   print   int   eve   返回   參數   

簡介 
Python 字典 update() 方法用於更新字典中的鍵/值對,可以修改存在的鍵對應的值,也可以添加新的鍵/值對到字典中。

文法 
d.update(e)

參數說明 
將e中鍵-值對添加到字典d中,e可能是字典,也可能是鍵-值對序列。詳見執行個體。

傳回值 
該方法沒有任何傳回值。

執行個體 
以下執行個體展示了 update() 方法的使用方法:

d = {‘one’:1,’two’:2}

d.update({‘three’:3,’four’:4}) # 傳一個字典 
print(d)

d.update(five=5,six=6) # 傳關鍵字 
print(d)

d.update([(‘seven’,7),(‘eight’,8)]) # 傳一個包含一個或多個元組的列表 
print(d)

d.update(([‘nice’,9],[‘ten’,10])) # 傳一個包含一個或多個列表的元組 
print(d)

d.update(zip([‘eleven’,’twelve’],[11,12])) # 傳一個zip()函數 
print(d)

d.update(one=111,two=222) # 使用以上任意方法修改存在的鍵對應的值 
print(d)

以上執行個體輸出結果為:

{‘one’: 1, ‘four’: 4, ‘three’: 3, ‘two’: 2} 
{‘one’: 1, ‘four’: 4, ‘three’: 3, ‘five’: 5, ‘two’: 2, ‘six’: 6} 
{‘seven’: 7, ‘one’: 1, ‘four’: 4, ‘three’: 3, ‘five’: 5, ‘two’: 2, ‘six’: 6, ‘eight’: 8} 
{‘seven’: 7, ‘one’: 1, ‘four’: 4, ‘three’: 3, ‘ten’: 10, ‘five’: 5, ‘nice’: 9, ‘two’: 2, ‘six’: 6, ‘eight’: 8} 
{‘one’: 1, ‘four’: 4, ‘three’: 3, ‘twelve’: 12, ‘ten’: 10, ‘seven’: 7, ‘six’: 6, ‘eleven’: 11, ‘two’: 2, ‘nice’: 9, ‘five’: 5, ‘eight’: 8} 
{‘one’: 111, ‘four’: 4, ‘three’: 3, ‘twelve’: 12, ‘ten’: 10, ‘seven’: 7, ‘six’: 6, ‘eleven’: 11, ‘two’: 222, ‘nice’: 9, ‘five’: 5, ‘eight’: 8}

python 列表中的Update()函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.