如何在Python中向字典添加項目

介紹

字典是Python中的內置數據類型。字典是一個由鍵值對組成的序列。字典是可變對象,但字典的鍵是不可變的,且在每個字典內需要唯一。雖然沒有內置的添加方法,但有幾種方法可以向字典中添加和更新數據。在本文中,您將使用Python的賦值運算符、update()方法以及合併和更新字典運算符來添加和更新Python字典。

使用DigitalOcean App平台從GitHub部署您的Python應用程序。讓DigitalOcean專注於應用程序的擴展。

四種向Python字典添加數據的方法

  1. 使用賦值運算符
  2. 使用更新方法
  3. 使用合併運算子
  4. 使用更新運算子

使用 = 賦值運算子將內容新增至 Python 字典

您可以使用 = 賦值運算子將新的鍵值對添加至字典中:

dict[key] = value

如果字典中已經存在該鍵,則賦值運算子會更新或覆寫該鍵的值。

以下示例演示如何創建一個新的字典,然後使用賦值運算子 = 更新值和添加鍵值對:

dict_example = {'a': 1, 'b': 2}

print("original dictionary: ", dict_example)

dict_example['a'] = 100  # 現有鍵,覆寫
dict_example['c'] = 3  # 新鍵,添加
dict_example['d'] = 4  # 新鍵,添加

print("updated dictionary: ", dict_example)

輸出為:

Output
original dictionary: {'a': 1, 'b': 2} updated dictionary: {'a': 100, 'b': 2, 'c': 3, 'd': 4}

輸出顯示 a 的值被新值覆寫,b 的值保持不變,並且對於 cd 添加了新的鍵值對。

將值添加到Python字典而不覆蓋已有值

使用=賦值運算符會覆蓋已有鍵的值。如果你知道你的程序可能有重複的鍵,但你不想覆蓋原始值,那麼可以使用if語句有條件地添加值。

繼續使用前一節的示例,可以使用if語句僅向字典添加新的鍵-值對:

dict_example = {'a': 1, 'b': 2}

print("original dictionary: ", dict_example)

dict_example['a'] = 100  # 既有鍵,覆蓋
dict_example['c'] = 3  # 新鍵,添加
dict_example['d'] = 4  # 新鍵,添加

print("updated dictionary: ", dict_example)

# 添加以下if語句

if 'c' not in dict_example.keys():
    dict_example['c'] = 300

if 'e' not in dict_example.keys():
    dict_example['e'] = 5

print("conditionally updated dictionary: ", dict_example)

輸出為:

Output
original dictionary: {'a': 1, 'b': 2} updated dictionary: {'a': 100, 'b': 2, 'c': 3, 'd': 4} conditionally updated dictionary: {'a': 100, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

輸出顯示,由於if條件,當條件更新字典時,c的值沒有改變。

使用update()方法將值添加到Python字典

您可以使用 update() 方法將字典或鍵值對的可迭代對象追加到字典中。 update() 方法會使用新值覆蓋現有鍵的值。

以下示例演示了如何創建一個新字典,使用 update() 方法添加新的鍵值對和新字典,並打印每個結果:

site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary'}
print("original dictionary: ", site)

# 使用作者鍵值對更新字典
site.update({'Author':'Sammy Shark'})
print("updated with Author: ", site)

# 創建一個新字典
guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}

# 使用新字典更新原字典
site.update(guests)
print("updated with new dictionary: ", site)

輸出結果為:

Output
original dictionary: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary'} updated with Author: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy Shark'} updated with new dictionary: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy Shark', 'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'}

輸出結果顯示第一個更新添加了一個新的鍵值對,第二個更新將 guest 字典的鍵值對添加到 site 字典中。請注意,如果您將一個已存在的鍵更新到字典中,則舊值將被更新覆蓋。

使用合併運算符 | 將內容添加到 Python 字典

您可以使用字典合併運算符 |(表示為管道字符)將兩個字典合併並返回一個新的字典對象。

以下示例演示如何创建两个字典,并使用合并运算符创建一个包含两个字典的新字典:

site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'}

guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}

new_site = site | guests

print("site: ", site)
print("guests: ", guests)
print("new_site: ", new_site)

输出为:

Output
site: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy'} guests: {'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'} new_site: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy', 'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'}

两个字典被合并为一个新的字典对象,其中包含来自两个字典的键值对。

如果一个键在两个字典中都存在,则取第二个字典或右操作数的值。在以下示例代码中,两个字典都有一个名为b的键:

dict1 = {'a':'one', 'b':'two'}
dict2 = {'b':'letter two', 'c':'letter three'}

dict3 = dict1 | dict2

print{"dict3: ", dict3}

输出为:

Output
dict3: {'a': 'one', 'b': 'letter two', 'c': 'letter three'}

b的值被来自右操作数dict2的值覆盖。

使用更新|=运算符添加到Python字典

您可以使用字典更新|=运算符(由管道和等号字符表示)以给定的字典或值就地更新字典。

就像合并|运算符一样,如果一个键在两个字典中都存在,则更新|=运算符取右操作数的值。

以下示例演示如何创建两个字典,使用更新运算符将第二个字典附加到第一个字典中,然后打印更新后的字典:

site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'}

guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}

site |= guests

print("site: ", site)

输出为:

Output
site: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy', 'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'}

在上面的示例中,您不需要创建第三个字典对象,因为更新操作符会修改原始对象。输出显示,guests字典被附加到了原始的site字典中。

結論

在本文中,您使用了不同的方法来添加和更新Python字典。继续学习更多的Python教程

Source:
https://www.digitalocean.com/community/tutorials/python-add-to-dictionary