如何在Python中添加到字典

介绍

字典是Python中的内置数据类型。字典是一系列键-值对。字典是可变对象,但是字典的键是不可变的,在每个字典中需要是唯一的。没有内置的添加方法,但有几种方法可以添加和更新字典。在本文中,您将使用Python赋值运算符、update()方法以及合并和更新字典运算符来添加和更新Python字典。

使用DigitalOcean 应用平台从GitHub部署您的Python应用程序。让DigitalOcean专注于扩展您的应用。

四种向Python字典添加元素的方法

  1. 使用赋值运算符
  2. 使用update方法
  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'}

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

如果一个键同时存在于两个字典中,那么取自第二个字典或右操作数的值。

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

dict3 = dict1 | dict2

print{"dict3: ", dict3}

在下面的示例代码中,两个字典都有一个名为b的键:

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