将字典附加到Python的列表
在本文中,我们将讨论如何在Python中将字典附加到列表数据结构中。
在这里我们将讨论:
- 将字典附加到具有相同键和不同值的列表中
- 使用 append() 方法
- 使用 copy() 方法使用 append() 方法列出
- 使用 deepcopy() 方法列出使用 append() 方法
- 使用 NumPy。
方法 1:将字典附加到具有相同键和不同值的列表中
在这里,我们将使用具有相同键但不同值的 for 循环将整数类型的字典附加到空列表中。我们将使用 using zip()函数
Syntax: list=[dict(zip([key],[x])) for x in range(start,stop)]
where, key is the key and range() is the range of values to be appended
示例:将 1 到 100 之间的 100 个值附加到列表中的 1 作为键的Python代码
在这个例子中,我们将 1 到 100 的元素作为值并将这些值分配给键 1,最后,我们压缩元素,最后,我们将元素附加到列表中。
Python3
# append 100 values from 1 to 100
# with 1 as key to list
l = [dict(zip([1],[x])) for x in range(1,100)]
# display list
print(l)
Python3
# create an empty list
l = []
# create a dictionary wih student details
student = {7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to the empty list
l.append(student)
# display list
l
Python3
# create an list that contain some elements
l = [1, 2, "hi", "welcome"]
# create a dictionary wih student details
student={7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to the empty list
l.append(student)
# display list
l
Python3
# create an empty list
l = []
# create a dictionary wih student details
student = {7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to the
# empty list using copy() method
l.append(student.copy())
# display list
l
Python3
# create an list that contain some elements
l = [1, 2, "hi", "welcome"]
# create a dictionary wih student details
student = {7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to
# the empty list using copy() method
l.append(student.copy())
# display list
l
Python3
# import deepcopy module
from copy import deepcopy
# create an empty list
l = []
# create a dictionary wih student details
student = {7058: 'sravan kumsr Gottumukkala',
7059: 'ojaswi', 7060: 'bobby',
7061: 'gnanesh', 7062: 'rohith'}
# append this dictionary to
# the empty list using deepcopy() method
l.append(deepcopy(student))
# display list
l
Python
# import deepcopy module
from copy import deepcopy
# create an list that contain some elements
l = [1, 2, "hi", "welcome"]
# create a dictionary wih student details
student = {7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to the empty
# list using deepcopy() method
l.append(deepcopy(student))
# display list
l
Python3
# import numpy library
import numpy as np
# define a list
subjects = ["PHP", "Java", "SQL"]
# iterating the elements in
# list to create an numpy array
data = np.array([{'GFG': i} for i in subjects])
# append to the numpy array to list
final = np.append(res_array, {'GFG': "ML/DL"}).tolist()
# Printing the appended data
print(final)
输出:
[{1: 1}, {1: 2}, {1: 3}, {1: 4}, {1: 5}, {1: 6}, {1: 7}, {1: 8}, {1: 9}, {1: 10}, {1: 11}, {1: 12}, {1: 13}, {1: 14}, {1: 15}, {1: 16}, {1: 17}, {1: 18}, {1: 19}, {1: 20}, {1: 21}, {1: 22}, {1: 23}, {1: 24}, {1: 25}, {1: 26}, {1: 27}, {1: 28}, {1: 29}, {1: 30}, {1: 31}, {1: 32}, {1: 33}, {1: 34}, {1: 35}, {1: 36}, {1: 37}, {1: 38}, {1: 39}, {1: 40}, {1: 41}, {1: 42}, {1: 43}, {1: 44}, {1: 45}, {1: 46}, {1: 47}, {1: 48}, {1: 49}, {1: 50}, {1: 51}, {1: 52}, {1: 53}, {1: 54}, {1: 55}, {1: 56}, {1: 57}, {1: 58}, {1: 59}, {1: 60}, {1: 61}, {1: 62}, {1: 63}, {1: 64}, {1: 65}, {1: 66}, {1: 67}, {1: 68}, {1: 69}, {1: 70}, {1: 71}, {1: 72}, {1: 73}, {1: 74}, {1: 75}, {1: 76}, {1: 77}, {1: 78}, {1: 79}, {1: 80}, {1: 81}, {1: 82}, {1: 83}, {1: 84}, {1: 85}, {1: 86}, {1: 87}, {1: 88}, {1: 89}, {1: 90}, {1: 91}, {1: 92}, {1: 93}, {1: 94}, {1: 95}, {1: 96}, {1: 97}, {1: 98}, {1: 99}]
方法二:使用append()方法
在这里,我们将使用 append() 方法将字典附加到列表中,该方法用于在列表中附加项目。
Syntax: list.append(dictionary)
where,
- list is the input list
- dictionary is an input dictionary to be appended
示例 1:将字典附加到空列表的Python代码
在这里,我们将学生 ID 视为键,将姓名视为字典,因此我们使用 append 方法将空列表附加到学生字典中
蟒蛇3
# create an empty list
l = []
# create a dictionary wih student details
student = {7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to the empty list
l.append(student)
# display list
l
输出:
[{7058: 'sravan kumsr Gottumukkala',
7059: 'ojaswi',
7060: 'bobby',
7061: 'gnanesh',
7062: 'rohith'}]
示例 2:将字典附加到包含元素的列表中
在这里,我们将学生 id 视为键,将姓名视为字典。所以我们使用 append 方法将已经包含一些元素的列表附加到学生字典中
蟒蛇3
# create an list that contain some elements
l = [1, 2, "hi", "welcome"]
# create a dictionary wih student details
student={7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to the empty list
l.append(student)
# display list
l
输出:
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
7059: 'ojaswi',
7060: 'bobby',
7061: 'gnanesh',
7062: 'rohith'}]
方法 3:使用copy()和 append() 方法
在这里,我们将使用 append() 方法将字典附加到列表中,我们可以使用 copy() 附加到列表
Syntax: dictionary.copy()
示例 1:使用 copy() 方法将字典附加到空列表的Python代码
在这里,我们将学生 id 视为键,将名称视为字典,因此我们使用复制方法将学生字典附加到使用 append 方法的空列表中
蟒蛇3
# create an empty list
l = []
# create a dictionary wih student details
student = {7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to the
# empty list using copy() method
l.append(student.copy())
# display list
l
输出:
[{7058: 'sravan kumsr Gottumukkala',
7059: 'ojaswi',
7060: 'bobby',
7061: 'gnanesh',
7062: 'rohith'}]
示例 2:使用 copy() 方法附加字典以列出包含元素的内容
在这里,我们将学生 id 视为键,将名称视为字典,因此我们使用 copy 方法将已经包含元素的列表附加到学生字典中,使用 append 方法
蟒蛇3
# create an list that contain some elements
l = [1, 2, "hi", "welcome"]
# create a dictionary wih student details
student = {7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to
# the empty list using copy() method
l.append(student.copy())
# display list
l
输出:
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
7059: 'ojaswi',
7060: 'bobby',
7061: 'gnanesh',
7062: 'rohith'}]
方法四:使用 deepcopy() 方法和 append() 方法
深拷贝是复制过程递归发生的过程,这里我们将通过深拷贝字典的方式将字典附加到列表中。
Syntax: append(deepcopy())
示例 1:使用深拷贝将字典追加到列表中
在这里,我们将学生 id 视为键,将名称视为值作为字典,因此我们使用 deepcopy 方法将学生字典附加到使用 append 方法的空列表中
蟒蛇3
# import deepcopy module
from copy import deepcopy
# create an empty list
l = []
# create a dictionary wih student details
student = {7058: 'sravan kumsr Gottumukkala',
7059: 'ojaswi', 7060: 'bobby',
7061: 'gnanesh', 7062: 'rohith'}
# append this dictionary to
# the empty list using deepcopy() method
l.append(deepcopy(student))
# display list
l
输出:
[{7058: 'sravan kumsr Gottumukkala',
7059: 'ojaswi',
7060: 'bobby',
7061: 'gnanesh',
7062: 'rohith'}]
示例 2:将字典追加到包含元素的列表中
在这里,我们将学生 id 视为键,将名称视为值作为字典,因此我们使用 deepcopy 方法将已包含元素的列表附加到学生字典中,使用 append 方法
Python
# import deepcopy module
from copy import deepcopy
# create an list that contain some elements
l = [1, 2, "hi", "welcome"]
# create a dictionary wih student details
student = {7058:'sravan kumsr Gottumukkala',
7059:'ojaswi',7060:'bobby',
7061:'gnanesh',7062:'rohith'}
# append this dictionary to the empty
# list using deepcopy() method
l.append(deepcopy(student))
# display list
l
输出:
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
7059: 'ojaswi',
7060: 'bobby',
7061: 'gnanesh',
7062: 'rohith'}]
方法五:使用Numpy
Numpy 代表用于存储和处理数组的数字Python ,这里我们将使用 NumPy 来附加字典。
Syntax: np.append(res_array, {‘key’: “value”}).tolist()
where,
- res_array is the resultabt array
- append is used to append to array
- tolist() is used to convert a list
示例:使用 NumPy 方法将字典附加到列表的Python代码。
在这里,我们创建了一个包含主题的元素列表,并传递 GFG 键并附加到列表中
蟒蛇3
# import numpy library
import numpy as np
# define a list
subjects = ["PHP", "Java", "SQL"]
# iterating the elements in
# list to create an numpy array
data = np.array([{'GFG': i} for i in subjects])
# append to the numpy array to list
final = np.append(res_array, {'GFG': "ML/DL"}).tolist()
# Printing the appended data
print(final)
输出:
[{‘GFG’: ‘PHP’}, {‘GFG’: ‘Java’}, {‘GFG’: ‘SQL’}, {‘GFG’: ‘ML/DL’}]