如何在特定条件下遍历 2 个压缩的字典列表?
How to iterate through 2 zipped list of dictionaries under certain conditions?
有4个字典列表,前两个需要添加到数据库中,第二个2个已经存在于数据库中:
to_add1 = [{'name': 'Kate', 'age': 25, 'id': 1234},
{'name': 'Claire', 'age': 25, 'id': 4567},
{'name': 'Bob', 'age': 25, 'id': 8910}]
to_add2 = [{'pets': 5, 'name_id': 1234},
{'pets': 0, 'name_id': 4567},
{'pets': 0, 'name_id': 8910}]
existing1 = [{'name': 'John', 'age': 50, 'id': 0000},
{'name': 'Claire', 'age': 25, 'id': 4567}]
existing2 = [{'pets': 2, 'name_id': 0000},
{'pets': 0, 'name_id': 4567}]
我想添加到数据库中,或者在这个可重现的示例中,只打印 existing1
中不包含 existing1['name']
值的项目。所以在这种情况下:来自 to_add1
的 Kate 和 Bob 应该打印一次。由于双循环,我得到重复。我怎样才能遍历所有这些字典并打印出与 existing 1
的名称匹配的项目而不重复?
我的代码:
for person_to_add, pet_to_add in zip(to_add1, to_add2):
for existing_person, existing_pet in zip(existing1, existing2):
if person_to_add['name'] not in existing_person['name']:
print(person_to_add)
当前输出:
{'name': 'Kate', 'age': 25, 'id': 1234}
{'name': 'Kate', 'age': 25, 'id': 1234}
{'name': 'Claire', 'age': 25, 'id': 4567}
{'name': 'Bob', 'age': 25, 'id': 8910}
{'name': 'Bob', 'age': 25, 'id': 8910}
期望输出:
{'name': 'Kate', 'age': 25, 'id': 4567}
{'name': 'Bob', 'age': 25, 'id': 8910}
我假设你的意思是输出应该是 Kate
& Bob
因为 Claire
在 existing1
:
>>> for person_to_add, pet_to_add in zip(to_add1, to_add2):
... if person_to_add not in existing1:
... print(person_to_add)
...
{'name': 'Kate', 'age': 25, 'id': 1234}
{'name': 'Bob', 'age': 25, 'id': 8910}
根据实际底层查询的复杂性,这可能行不通。但这是一个利用您正在使用的字典结构列表的解决方案。我不清楚为什么你需要使用 zip。
a = [person_to_add for person_to_add in to_add1 if person_to_add['name'] not in [existing_person['name'] for existing_person in existing1]]
这使用了列表推导式,应该非常快。输出 'a' 的格式与所有其他数据的格式相同,即字典列表。如果您需要进一步了解列表理解的工作原理,请告诉我。
lst = [print(person_to_add) for person_to_add in to_add1 if person_to_add not in existing1]
它可能不是最快的解决方案,但它可以完成工作。它使用 python 列表推导式,易于阅读。
正如您在问题中所述,您只想比较 existing1 和 to_add1 中的数据,因此您不需要使用嵌套循环来增加不必要的复杂性。使用单个循环就可以了。
有4个字典列表,前两个需要添加到数据库中,第二个2个已经存在于数据库中:
to_add1 = [{'name': 'Kate', 'age': 25, 'id': 1234},
{'name': 'Claire', 'age': 25, 'id': 4567},
{'name': 'Bob', 'age': 25, 'id': 8910}]
to_add2 = [{'pets': 5, 'name_id': 1234},
{'pets': 0, 'name_id': 4567},
{'pets': 0, 'name_id': 8910}]
existing1 = [{'name': 'John', 'age': 50, 'id': 0000},
{'name': 'Claire', 'age': 25, 'id': 4567}]
existing2 = [{'pets': 2, 'name_id': 0000},
{'pets': 0, 'name_id': 4567}]
我想添加到数据库中,或者在这个可重现的示例中,只打印 existing1
中不包含 existing1['name']
值的项目。所以在这种情况下:来自 to_add1
的 Kate 和 Bob 应该打印一次。由于双循环,我得到重复。我怎样才能遍历所有这些字典并打印出与 existing 1
的名称匹配的项目而不重复?
我的代码:
for person_to_add, pet_to_add in zip(to_add1, to_add2):
for existing_person, existing_pet in zip(existing1, existing2):
if person_to_add['name'] not in existing_person['name']:
print(person_to_add)
当前输出:
{'name': 'Kate', 'age': 25, 'id': 1234}
{'name': 'Kate', 'age': 25, 'id': 1234}
{'name': 'Claire', 'age': 25, 'id': 4567}
{'name': 'Bob', 'age': 25, 'id': 8910}
{'name': 'Bob', 'age': 25, 'id': 8910}
期望输出:
{'name': 'Kate', 'age': 25, 'id': 4567}
{'name': 'Bob', 'age': 25, 'id': 8910}
我假设你的意思是输出应该是 Kate
& Bob
因为 Claire
在 existing1
:
>>> for person_to_add, pet_to_add in zip(to_add1, to_add2):
... if person_to_add not in existing1:
... print(person_to_add)
...
{'name': 'Kate', 'age': 25, 'id': 1234}
{'name': 'Bob', 'age': 25, 'id': 8910}
根据实际底层查询的复杂性,这可能行不通。但这是一个利用您正在使用的字典结构列表的解决方案。我不清楚为什么你需要使用 zip。
a = [person_to_add for person_to_add in to_add1 if person_to_add['name'] not in [existing_person['name'] for existing_person in existing1]]
这使用了列表推导式,应该非常快。输出 'a' 的格式与所有其他数据的格式相同,即字典列表。如果您需要进一步了解列表理解的工作原理,请告诉我。
lst = [print(person_to_add) for person_to_add in to_add1 if person_to_add not in existing1]
它可能不是最快的解决方案,但它可以完成工作。它使用 python 列表推导式,易于阅读。
正如您在问题中所述,您只想比较 existing1 和 to_add1 中的数据,因此您不需要使用嵌套循环来增加不必要的复杂性。使用单个循环就可以了。