gpt4 book ai didi

python - 字典结构(dict -> dict)与比较中的列表

转载 作者:行者123 更新时间:2023-12-01 09:23:09 28 4
gpt4 key购买 nike

有这个字典 -> 字典 -> 列表结构

想要比较这种类型的 2 个结构。

one = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "0.0.0.0/0"]}}
two = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "Blue", "0.0.0.0/0"]}}

这段代码运行良好:

def compare(one,two):
for mainkey in one:
for subkey in one[mainkey]:
return set(one[mainkey][subkey]) ^ set(two[mainkey][subkey])

但是,当 dict -> dict 有更多或更少的键时,它应该由添加或删除键以及所有列表值的函数返回。

此外,如果列表被修改,则列表被修改的程序应该返回它。

有人可以帮忙吗?

它几乎用于比较两个 JSON,我想查看何时删除、添加键或修改其值。

更新1:

我还在学习Python

对于这个结构:

one = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "0.0.0.0/0"]}}
two = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "Blue", "0.0.0.0/0"]},"119": ["test10","test11"]}

它不起作用。

它应该打印为输出:

118 was modified. New values Blue. 119 was added with values test10 test11

对于这些场景:

1.

one = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "0.0.0.0/0"]}}
two = {"1iG5NDGVre": }

应打印为输出:

118 was removed with values test1 test2 test3 tcp 22 Red 0.0.0.0/0

2.

one = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "0.0.0.0/0"]}}
two = {"1iG5NDGVre": {"118": ["test100", "test200", "test3", "tcp", "22", "Red", "Blue", "0.0.0.0/0"]},"119": ["test10","test11"]}

应打印为输出:

118 was modifed. New values test100 test200

我想涵盖所有可能的情况。我按照我所说的 JSON 比较来执行此操作。

最佳答案

我在您的字典中添加了一些子项,以便为每种情况提供一个示例:

one = {"1iG5NDGVre": {"116": ["commonkey1", "commonkey2"], "118": ["test1", "test2", "test3", "tcp", "22", "Red", "0.0.0.0/0"],"117": ["test4", "test5", "test6", "tcp", "42", "Fucsia", "0.0.0.0/0"]}}
two = {"1iG5NDGVre": {"116": ["commonkey1", "commonkey2"], "118": ["test100", "test200", "test3", "tcp", "22", "Red", "Blue", "0.0.0.0/0"],"119": ["test10","test11"]}}

地点:

  • 116 存在于两者中,未修改
  • 118 存在于两者中,已修改
  • 117 仅出现在一个
  • 119 仅出现在两个

然后我们迭代我们的字典:

def compare(one,two):
for mainkey in one:
# Here we are iterating at "1iG5NDGVre" key level
# We want to know the keys which has been added, removed, and modified
# keys removed:
for key in set(one[mainkey].keys()).difference(two[mainkey].keys()):
print "{0} was removed. Removed values: {1}".format(key, one[mainkey][key])
# keys added:
for key in set(two[mainkey].keys()).difference(one[mainkey].keys()):
print "{0} was added. Added values: {1}".format(key, two[mainkey][key])
# keys modified
for key in set(one[mainkey].keys()).intersection(two[mainkey].keys()):
if set(one[mainkey][key]) ^ set(two[mainkey][key]): print("{0} was modified. New values {1}".format(key, set(one[mainkey][key]) ^ set(two[mainkey][key])))


compare(one,two)
# OUTPUT:
# 117 was removed. Removed values: ['test4', 'test5', 'test6', 'tcp', '42', 'Fucsia', '0.0.0.0/0']
# 119 was added. Added values: ['test10', 'test11']
# 118 was modified. New values set(['Blue', 'test1', 'test2', 'test100', 'test200'])

这里发生了什么:

set(one[mainkey].keys()).difference(two[mainkey].keys()) # returns 117, aka what is present in 'one' but not in 'two'

set(two[mainkey].keys()).difference(one[mainkey].keys()) # returns 119, aka what is present in 'two' but not in 'one'

set(one[mainkey].keys()).intersection(two[mainkey].keys()) # returns 116, 118, aka keys present in both

请注意,当我们检查两者中存在的元素时,我们总是返回一些内容:如果值相等,则返回一个空列表[],或者返回一个具有不同值的列表。

此外,我们使用,它只接受唯一值:

set(["a", "a", "b", "b", "b", "c"]) # returns ("a", "b", "c").

这对于字典来说不是问题,因为键也是唯一的,但可能会给列表带来一些问题。如果您想解决此问题,可以使用 list comprehension ,这也是改进之前代码的好方法。我建议您也看看Python set operation

关于python - 字典结构(dict -> dict)与比较中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50643953/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com