📜  Python – 在字典列表中添加自定义值键

📅  最后修改于: 2022-05-13 01:55:31.283000             🧑  作者: Mango

Python – 在字典列表中添加自定义值键

给定一个字典列表、自定义列表和键,将键按顺序添加到每个字典的列表值。

方法 #1:使用循环 + enumerate()

这是可以执行此任务的方式之一。在此,我们使用 enumerate() 遍历字典以获取索引,并使用其索引值继续分配给每个字典键。

Python3
# Python3 code to demonstrate working of 
# Add custom values key in List of dictionaries
# Using loop
  
# initializing lists
test_list = [{"Gfg" : 6, "is" : 9, "best" : 10}, 
             {"Gfg" : 8, "is" : 11, "best" : 19},
             {"Gfg" : 2, "is" : 16, "best" : 10},
             {"Gfg" : 12, "is" : 1, "best" : 8},
             {"Gfg" : 22, "is" : 6, "best" : 8}]
  
# printing original list
print("The original list : " + str(test_list))
  
# initializing Key 
K = "CS"
  
# initializing list 
append_list = [6, 7, 4, 3, 9]
  
# using enumerate() to iterate for index and values
for idx, ele in enumerate(test_list):
        ele[K] = append_list[idx]
  
# printing result 
print("The dictionary list after addition : " + str(test_list))


Python3
# Python3 code to demonstrate working of 
# Add custom values key in List of dictionaries
# Using zip() + loop
  
# initializing lists
test_list = [{"Gfg" : 6, "is" : 9, "best" : 10}, 
             {"Gfg" : 8, "is" : 11, "best" : 19},
             {"Gfg" : 2, "is" : 16, "best" : 10},
             {"Gfg" : 12, "is" : 1, "best" : 8},
             {"Gfg" : 22, "is" : 6, "best" : 8}]
  
# printing original list
print("The original list : " + str(test_list))
  
# initializing Key 
K = "CS"
  
# initializing list 
append_list = [6, 7, 4, 3, 9]
  
# zip() used to bind index wise 
# list and dictionary
for dic, lis in zip(test_list, append_list):
  dic[K] = lis
  
# printing result 
print("The dictionary list after addition : " + str(test_list))


输出
The original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The dictionary list after addition : [{'Gfg': 6, 'is': 9, 'best': 10, 'CS': 6}, {'Gfg': 8, 'is': 11, 'best': 19, 'CS': 7}, {'Gfg': 2, 'is': 16, 'best': 10, 'CS': 4}, {'Gfg': 12, 'is': 1, 'best': 8, 'CS': 3}, {'Gfg': 22, 'is': 6, 'best': 8, 'CS': 9}]

方法 #2:使用 zip() + 循环

这是可以执行此任务的另一种方式。在此,我们使用 zip() 执行列表值与每个字典的映射。

Python3

# Python3 code to demonstrate working of 
# Add custom values key in List of dictionaries
# Using zip() + loop
  
# initializing lists
test_list = [{"Gfg" : 6, "is" : 9, "best" : 10}, 
             {"Gfg" : 8, "is" : 11, "best" : 19},
             {"Gfg" : 2, "is" : 16, "best" : 10},
             {"Gfg" : 12, "is" : 1, "best" : 8},
             {"Gfg" : 22, "is" : 6, "best" : 8}]
  
# printing original list
print("The original list : " + str(test_list))
  
# initializing Key 
K = "CS"
  
# initializing list 
append_list = [6, 7, 4, 3, 9]
  
# zip() used to bind index wise 
# list and dictionary
for dic, lis in zip(test_list, append_list):
  dic[K] = lis
  
# printing result 
print("The dictionary list after addition : " + str(test_list))
输出
The original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The dictionary list after addition : [{'Gfg': 6, 'is': 9, 'best': 10, 'CS': 6}, {'Gfg': 8, 'is': 11, 'best': 19, 'CS': 7}, {'Gfg': 2, 'is': 16, 'best': 10, 'CS': 4}, {'Gfg': 12, 'is': 1, 'best': 8, 'CS': 3}, {'Gfg': 22, 'is': 6, 'best': 8, 'CS': 9}]