Python 在字典中追加列表

Python 在字典中追加列表

有时,我们可能需要将列表存储为字典键的值,并且在将来的某个时候,由于某种原因我们需要更新或添加更多元素到该列表中。Python提供了几种方法来向Python字典中的列表追加元素。在本文中,我们将使用内置方法’append()’、’update()’和’+=’运算符来实现此目的。

在Python字典中追加列表的程序

在直接进入程序之前,让我们先熟悉一下Python中的列表和字典。

列表

它是一个有序且可变的元素集合。它类似于C/C++和Java编程语言中的数组。我们可以通过将元素放在方括号'[]’中,并用逗号分隔来创建一个列表。

实例

Fruitlist = ["apple", "banana", "orange"]

字典

它是一个无序且可变的元素集合。它以键值对的方式存储其中的元素,每个键与一个值相关联。我们可以通过将元素放置在花括号‘{ }’中并使用冒号‘:’分隔每对键值对来创建一个字典。

实例

Details = {"Tutor" : "Tutorialspoint", "Rank" : 01, "country" : "India"}

我们将使用以下方法来在Python字典中追加列表:

使用append()方法

该方法接受一个参数并将其添加到指定列表的末尾。其语法如下:

语法

dictionary["key"].append("value")

方法

  • 使用三个列表初始化一个名为’hobbies’的字典。它将姓名映射到爱好。

  • 使用append方法向每个现有列表添加一个元素。

  • 现在,打印新的字典并退出。

示例

hobbies = {
   "Ram": ["reading", "painting"],
   "Shyam": ["gaming", "coding"],
   "Shiv": ["cooking", "biking"]
}
print("The original list of hobbies: ")
print(hobbies)
# Appending new elements to the list in dictionary
hobbies["Ram"].append("singing")
hobbies["Shyam"].append("chess")
hobbies["Shiv"].append("gardening")
print("New list of hobbies: ")
print(hobbies)

输出

The original list of hobbies: 

{'Ram': ['reading', 'painting'], 'Shyam': ['gaming', 'coding'], 
'Shiv': ['cooking', 'biking']}
New list of hobbies: 
{'Ram': ['reading', 'painting', 'singing'], 'Shyam': ['gaming', 
'coding', 'chess'], 'Shiv': ['cooking', 'biking', 'gardening']}

使用+=运算符

作为替代,我们可以使用’+=’运算符代替’append()’方法将元素添加到给定Python字典的列表中。此运算符等同于将右操作数添加到左操作数并将结果重新赋值给左操作数。

示例

hobbies = {
   "Ram": ["reading", "painting"],
   "Shyam": ["gaming", "coding"],
   "Shiv": ["cooking", "biking"]
}
print("The original list of hobbies: ")
print(hobbies)
# Appending new elements to the list in dictionary
hobbies["Ram"] += ["singing"]
hobbies["Shyam"] += ["chess"]
hobbies["Shiv"] += ["gardening"]
print("New list of hobbies: ")
print(hobbies)

输出

The original list of hobbies: 

{'Ram': ['reading', 'painting'], 'Shyam': ['gaming', 'coding'], 
'Shiv': ['cooking', 'biking']}
New list of hobbies: 
{'Ram': ['reading', 'painting', 'singing'], 'Shyam': ['gaming', 
'coding', 'chess'], 'Shiv': ['cooking', 'biking', 'gardening']}
Note that we have to use the square brackets '[ ]' to wrap the 
element we want to append to a list. The reason behind this is 
the += operator expects both operands to be of the same type.

请注意,我们必须使用方括号 ‘[ ]’ 将要追加到列表中的元素包裹起来。这是因为 += 运算符要求两个操作数类型相同。

使用 update() 方法

该方法不返回任何值。它只是将一个指定的字典添加到另一个现有的字典中。它的语法如下:

语法:

dictionary1.update(dictionary2)

方法

  • 用一个空列表的值初始化一个名为’hobbies’的字典,有三个键。

  • 现在,使用update方法将值列表追加到现有的空列表中。

  • 现在,打印新的字典并退出。

示例

# creating dictionary with empty list
hobbies = {
   "Ram": [],
   "Shyam": [],
   "Shiv": []
}
print("The original list of hobbies: ")
print(hobbies)
# Appending new elements to the list in dictionary
hobbies.update({"Ram" : ["singing"]})
hobbies.update({"Shyam" : ["chess"]})
hobbies.update({"Shiv" : ["gardening"]})
print("New list of hobbies: ")
print(hobbies)

输出

The original list of hobbies: 
{'Ram': [], 'Shyam': [], 'Shiv': []}
New list of hobbies: 
{'Ram': ['singing'], 'Shyam': ['chess'], 'Shiv': ['gardening']}

结论

在本文中,我们学习了如何将新元素添加到Python字典中的现有列表中。我们看到了三种方法来执行这个操作:使用append()和update()方法,以及使用+=运算符。它们都会原地修改原始字典。我们还了解了Python中列表和字典的基础知识。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程