如何在 Python 中向字典添加内容

介绍

字典是一个内置的Python数据类型. 字典是关键值对的序列。 字典是可变的对象,但是,字典密钥是不可变的,并且需要在每个字典中独一无二。 没有内置的添加方法,但有几种方法可以添加和更新一个字典。 在本文中,您将使用Python分配操作员,更新( )的方法,并合并和更新字典操作员来添加和更新Python字典。

使用 = 分配操作器添加到字典

您可以使用 = 分配运算符将新密钥添加到字典中:

1dict[key] = value

如果字典中已经存在一个密钥,则分配运算器会更新或重写该值。

下面的示例展示了如何创建一个新的字典,然后使用分配运算符 = 来更新一个值并添加关键值对:

1dict_example = {'a': 1, 'b': 2}
2
3print("original dictionary: ", dict_example)
4
5dict_example['a'] = 100  # existing key, overwrite
6dict_example['c'] = 3  # new key, add
7dict_example['d'] = 4  # new key, add 
8
9print("updated dictionary: ", dict_example)

产量是:

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

输出显示a的值被新值重写,b的值保持不变,并为cd添加新的关键值对。

添加到字典中,而不写过值

如果您知道您的程序可能有重复键,但您不希望重写原始值,则可以使用如果语句随条件添加值。

继续从上一节的示例,您可以使用如果语句只添加新的关键值对到字典:

 1dict_example = {'a': 1, 'b': 2}
 2
 3print("original dictionary: ", dict_example)
 4
 5dict_example['a'] = 100  # existing key, overwrite
 6dict_example['c'] = 3  # new key, add
 7dict_example['d'] = 4  # new key, add 
 8
 9print("updated dictionary: ", dict_example)
10
11# add the following if statements
12
13if 'c' not in dict_example.keys():
14    dict_example['c'] = 300
15
16if 'e' not in dict_example.keys():
17    dict_example['e'] = 5
18
19print("conditionally updated dictionary: ", dict_example)

产量是:

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

输出显示,由于如果条件,当词典被条件更新时,c的值没有改变。

使用更新()方法添加到字典

您可以使用 update() 方法将字典或关键值对的迭代符附加到字典中。

下面的示例展示了如何创建一个新的字典,使用更新()方法添加一个新的密钥值对和一个新的字典,并打印每个结果:

 1site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary'}
 2print("original dictionary: ", site)
 3
 4# update the dictionary with the author key-value pair
 5site.update({'Author':'Sammy Shark'})
 6print("updated with Author: ", site)
 7
 8# create a new dictionary
 9guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}
10
11# update the original dictionary with the new dictionary
12site.update(guests)
13print("updated with new dictionary: ", site)

产量是:

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

输出显示,第一个更新会添加一个新的关键值对,第二个更新会将客人字典中的关键值对添加到网站字典中。

使用合并操作器添加到字典

您可以使用字典 merge 函数,以管符号表示,将两个字典合并并返回一个新的字典对象。

下面的示例展示了如何创建两个字典,并使用合并运算器创建一个包含两个关键值对的新字典:

1site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'}
2
3guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}
4
5new_site = site | guests
6
7print("site: ", site)
8print("guests: ", guests)
9print("new_site: ", new_site)

产量是:

1[secondary_label Output]
2site:  {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy'}
3guests:  {'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'}
4new_site:  {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy', 'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'}

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

如果两个字典中都存在一个密钥,那么第二个字典的值,或者是右操作符,就是所采用的值. 在下面的示例代码中,两个字典都有一个名为b的密钥:

1dict1 = {'a':'one', 'b':'two'}
2dict2 = {'b':'letter two', 'c':'letter three'}
3
4dict3 = dict1 | dict2
5
6print{"dict3: ", dict3}

产量是:

1[secondary_label Output]
2dict3:  {'a': 'one', 'b': 'letter two', 'c': 'letter three'}

关键b的值被右操作数dict2的值重写。

使用更新操作器添加到字典

您可以使用字典更新操作员=,以管道和等符号表示,以使用所述字典或值来更新字典。

就如同 merge 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数 函数。

下面的示例展示了如何创建两个字典,使用更新操作器将第二个字典附加到第一个字典,然后打印更新的字典:

1site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'}
2
3guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}
4
5site |= guests
6
7print("site: ", site)

产量是:

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

在上面的示例中,您不需要创建第三个字典对象,因为更新操作器会更改原始对象。

结论

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

Published At
Categories with 技术
Tagged with
comments powered by Disqus