我尝试在SO中进行一些搜索以找到解决方案,但是我仍然很困惑。我认为我从根本上误解了有关循环,列表和字典的一些东西。
我基本上是自学成才,绝不是专家,所以如果这是一个非常愚蠢的问题,请提前道歉。
在下面的代码片段中,我有各种字典列表,例如l1和l2样本。 我想要的输出是类似
l3 = [{'A':1, 'B':4},{'A':2,'B':5},{'A':3,'B':6}
但是,无论我尝试什么,我似乎总是只能从第二个字典中获得最后一个键值对,即
[{'A': 1, 'B': 6}, {'A': 2, 'B': 6}, {'A': 3, 'B': 6}]
这就是我所拥有的(评论以解释我在想/应该做的代码)
# First list of dictionaries
l1 = [{'A': 1},{'A': 2},{'A': 3}]
print(l1)
# Second list of dictionaries
l2 = [{'B': 4},{'B': 5},{'B': 6}]
print(l2)
# Empty list - to receive dictionaries in l1 and l2
l3 =[]
print(l3)
# Adding dictionaries from l1 into l3
for dict1 in l1:
l3.append(dict1)
print(l3)
# Opening l3 to loop through each dictionary, using range(len()) to loop through the index positions
for i in range(len(l3)):
# Opening l2 to loop through each dictionary
for dict2 in l2:
l3[i].update(dict2)
print(l3)
# Tried inverting the lists here, looping through l2 and then looping through l3 to append all dictionaries in l2
for dict2 in l2:
for i in range(len(l3)):
l3[i].update(dict2)
print(l3)
我也尝试使用zip()并得到了一个字典元组列表,这使我觉得我要么使用不正确,要么对于我在这里使用的工具来说太复杂了。 从我的研究工作中可以理解,问题是我一直压倒我刚刚写的值,我认为这就是为什么我总是在所有地方只添加最后一个值的原因。
任何帮助表示赞赏!
答案 0 :(得分:3)
您在这里输入的代码不正确。对于l3中的每个字典,您将更新l2中的每个字典。
valueRight = 100
phi.constrain(valueRight, mesh.facesRight)
尝试一下:
for i in range(len(l3)):
# Opening l2 to loop through each dictionary
for dict2 in l2:
l3[i].update(dict2)
这是一个更好的解决方案: 如果它们具有相同的键,则第二个列表字典值将覆盖第一个。
for i in range(len(l3)):
l3[i].update(l2[i])
答案 1 :(得分:1)
这应该在一行代码中就可以解决问题:
# First list of dictionaries
l1 = [{'A': 1},{'A': 2},{'A': 3}]
print(l1)
# Second list of dictionaries
l2 = [{'B': 4},{'B': 5},{'B': 6}]
print(l2)
# add dictionaries for dictionaries in zipped list1 and list2
l3 = [dict(d1, **d2) for d1, d2 in zip(l1, l2) ]
print(l3)
输出:
[{'A': 1}, {'A': 2}, {'A': 3}]
[{'B': 4}, {'B': 5}, {'B': 6}]
[{'A': 1, 'B': 4}, {'A': 2, 'B': 5}, {'A': 3, 'B': 6}]
答案 2 :(得分:0)
您的第一个问题在这里:
# Adding dictionaries from l1 into l3
for dict1 in l1:
l3.append(dict1)
print(l3)
您要将引用复制到第一个列表中的所有字典。它们与您的第一个列表中的相同。这意味着,编辑字典(例如调用update()
)将影响两个列表。
# Opening l3 to loop through each dictionary, using range(len()) to loop through the index positions
for i in range(len(l3)):
# Opening l2 to loop through each dictionary
for dict2 in l2:
l3[i].update(dict2)
print(l3)
# Tried inverting the lists here, looping through l2 and then looping through l3 to append all dictionaries in l2
for dict2 in l2:
for i in range(len(l3)):
l3[i].update(dict2)
print(l3)
在这里,您要遍历字典两次。 l3
是l1
的副本,对于列表中的每个项目,您都使用l2
中项目的每个中的数据进行更新。字典必须具有唯一的键,因此每次更新键a
和b
时,它都会覆盖以前的值。
然后,您基本上会执行相同的操作,但是顺序不同。
您要执行的操作将同时遍历每个列表中的字典,并根据每个列表中的数据创建新的字典。列表中每个位置的字典都有唯一的键,因此不会有任何覆盖。
l3 = [{**d1, **d2} for d1, d2 in zip(l1, l2)]
zip()
是在此处使用的正确功能。它从每个列表中取出一项,然后将它们作为一对递给您。一个列表和另一个列表中位于i
位置的项目。
>>> print(list(zip(l1, l2)))
[({'A': 1}, {'B': 4}), ({'A': 2}, {'B': 5}), ({'A': 3}, {'B': 6})]
因此,一旦有了它,就可以从源字典创建一个新字典。如您所知,{}
是新字典的语法,对变量执行**
意味着提取项目(键/值对)并将其插入到新字典中。
另一种较少使用python的语言,但对于新手来说,也许更容易编写:
l3 = []
for d1, d2 in zip(l1, l2):
d3 = {}
d3.update(d1)
d3.update(d2)
l3.append(d3)
希望您能体会第一个解决方案的简洁性。