如何使用另一个字典筛选字典?

huangapple go评论71阅读模式
英文:

How to filter a dictionary using another dictionary?

问题

我有两组字典。我们称它们为A和B。我需要能够创建第三个字典C,其中包含A的所有键和相关值,除了在B中的那些键值对。这是我到目前为止尝试过的:

A = {"A": {"B": 6, "C": 5}, "B": {"A": 6, "D": 7, "E": 5}, "C": {"A": 5}, "D": {"B": 7, "E": 10, "H": 25},
     "E": {"D": 10, "F": 14, "B": 5}, "F": {"E": 14}, "H": {"D": 25}}
B = {"A": "C"}

C = A

for key, value in A.items():
     C.update({key: value})
     if key in B:
          for i in value:
               if i not in B.values():
                    C[key].pop(i)

print(C)

思路是,给定这两个字典,得到输出:

A = {"A": {"B": 6}, "B": {"A": 6, "D": 7, "E": 5}, "C": {"A": 5}, "D": {"B": 7, "E": 10, "H": 25}, "E": {"D": 10, "F": 14, "B": 5}, "F": {"E": 14}, "H": {"D": 25}}

但是,终端返回错误,说在迭代过程中字典大小已经改变。我在这里到底做错了什么?

英文:

I have two sets of dictionaries. Let's call them A and B. I need to be able to create a third dictionary, C, which has all the keys and associated values of A, except for the pairs that are in B. This is what I have tried so far:

A = {"A": {"B": 6, "C": 5}, "B": {"A": 6, "D": 7, "E": 5}, "C": {"A": 5}, "D": {"B": 7, "E": 10, "H": 25},
     "E": {"D": 10, "F": 14, "B": 5}, "F": {"E": 14}, "H": {"D": 25}}
B = {"A": "C"}

C = A

for key, value in A.items():
     C.update({key: value})
     if key in B:
          for i in value:
               if i not in B.values():
                    C[key].pop(i)

print(C)

The idea is, given the two dictionaries, to obtain the output:

A = {"A": {"B": 6}, "B": {"A": 6, "D": 7, "E": 5}, "C": {"A": 5}, "D": {"B": 7, "E": 10, "H": 25}, "E": {"D": 10, "F": 14, "B": 5}, "F": {"E": 14}, "H": {"D": 25}}

Instead, the terminal returns an error, saying that the dictionary has changed size during the iteration. What exactly am I doing wrong here?

答案1

得分: 1

你可以使用字典推导式:

A = {
    "A": {"B": 6, "C": 5},
    "B": {"A": 6, "D": 7, "E": 5},
    "C": {"A": 5},
    "D": {"B": 7, "E": 10, "H": 25},
    "E": {"D": 10, "F": 14, "B": 5},
    "F": {"E": 14},
    "H": {"D": 25},
}
B = {"A": "C"}

C = {
    k: {kk: vv for kk, vv in v.items() if not (k in B and B[k] == kk)}
    for k, v in A.items()
}
print(C)

输出结果:

{
    "A": {"B": 6},
    "B": {"A": 6, "D": 7, "E": 5},
    "C": {"A": 5},
    "D": {"B": 7, "E": 10, "H": 25},
    "E": {"D": 10, "F": 14, "B": 5},
    "F": {"E": 14},
    "H": {"D": 25},
}
英文:

You can use dict comprehension:

A = {
    "A": {"B": 6, "C": 5},
    "B": {"A": 6, "D": 7, "E": 5},
    "C": {"A": 5},
    "D": {"B": 7, "E": 10, "H": 25},
    "E": {"D": 10, "F": 14, "B": 5},
    "F": {"E": 14},
    "H": {"D": 25},
}
B = {"A": "C"}

C = {
    k: {kk: vv for kk, vv in v.items() if not (k in B and B[k] == kk)}
    for k, v in A.items()
}
print(C)

Prints:

{
    "A": {"B": 6},
    "B": {"A": 6, "D": 7, "E": 5},
    "C": {"A": 5},
    "D": {"B": 7, "E": 10, "H": 25},
    "E": {"D": 10, "F": 14, "B": 5},
    "F": {"E": 14},
    "H": {"D": 25},
}

答案2

得分: 0

我认为你想要的是:

import pprint

A = {
    "A": {"B": 6, "C": 5},
    "B": {"A": 6, "D": 7, "E": 5},
    "C": {"A": 5},
    "D": {"B": 7, "E": 10, "H": 25},
    "E": {"D": 10, "F": 14, "B": 5},
    "F": {"E": 14},
    "H": {"D": 25},
}
B = {"A": "C"}
C = {}

for k, v in A.items():
    for kk, vv in v.items():
        if k in B and B[k] == kk:
            continue

        C.setdefault(k, {})[kk] = vv

pprint.pprint(C)

根据你的示例输入,会产生以下输出:

{'A': {'B': 6},
 'B': {'A': 6, 'D': 7, 'E': 5},
 'C': {'A': 5},
 'D': {'B': 7, 'E': 10, 'H': 25},
 'E': {'B': 5, 'D': 10, 'F': 14},
 'F': {'E': 14},
 'H': {'D': 25}}
英文:

I think you want:

import pprint

A = {
    "A": {"B": 6, "C": 5},
    "B": {"A": 6, "D": 7, "E": 5},
    "C": {"A": 5},
    "D": {"B": 7, "E": 10, "H": 25},
    "E": {"D": 10, "F": 14, "B": 5},
    "F": {"E": 14},
    "H": {"D": 25},
}
B = {"A": "C"}
C = {}

for k, v in A.items():
    for kk, vv in v.items():
        if k in B and B[k] == kk:
            continue

        C.setdefault(k, {})[kk] = vv

pprint.pprint(C)

Which given your sample input produces:

{'A': {'B': 6},
 'B': {'A': 6, 'D': 7, 'E': 5},
 'C': {'A': 5},
 'D': {'B': 7, 'E': 10, 'H': 25},
 'E': {'B': 5, 'D': 10, 'F': 14},
 'F': {'E': 14},
 'H': {'D': 25}}

huangapple
  • 本文由 发表于 2023年6月5日 00:34:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定