共計 256 個字符,預計需要花費 1 分鐘才能閱讀完成。
在 Python 中,可以使用 update()
方法來合并兩個字典。
示例代碼如下:
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
dict1.update(dict2)
print(dict1)
輸出結果為:
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
update()
方法將字典 dict2
中的鍵值對添加到字典 dict1
中,如果有相同的鍵,則會用 dict2
中的對應值進行覆蓋。
丸趣 TV 網 – 提供最優質的資源集合!
正文完