英文:
Keep items with same keys in two dictionary and discard other items
问题
我试图从两个字典dict_a
和dict_b
中删除所有不匹配的项(具有不同键的值)。有没有更好的方法来实现这个目标?
示例:
dict_a = {key1: x1, key2: y1, key4: z1}
dict_b = {key1: x2, key3: y2, key4: w1}
# 变成:
# dict_a = {key1: x1, key4: z1}
# dict_b = {key1: x2, key4: w1}
我的尝试:
keep_keys = [keyA for keyA in dict_a.keys() if keyA in dict_b.keys()]
for i in dict_b.copy().keys():
if i not in keep_keys:
del dict_b[i]
for i in dict_a.copy().keys():
if i not in keep_keys:
del dict_a[i]
我正在寻找更短(pythonic)的方法来完成这个任务。
英文:
I am trying to remove all non-matching items (values with different keys) in two dicts dict_a
and dict_b
. What is a better way of achieving this?
Example:
dict_a = {key1: x1, key2: y1, key4: z1}
dict_b = {key1: x2, key3: y2, key4: w1}
# becomes:
# dict_a = {key1: x1, key4: z1}
# dict_b = {key1: x2, key4: w1}
My attempt:
keep_keys = [keyA for keyA in dict_a.keys() if keyA in dict_b.keys()]
for i in dict_b.copy().keys():
if i not in keep_keys:
del dict_b[i]
for i in dict_a.copy().keys():
if i not in keep_keys:
del dict_a[i]
I am looking for shorter (pythonic) way of doing this.
答案1
得分: 3
这并不好太多,但这使用了所谓的“字典推导”来实现它。这实际上是一种一行的for
循环(但这里有两行,因为我们有两个字典)。
dict_a = {k: v for k, v in dict_a.items() if k in dict_b}
dict_b = {k: v for k, v in dict_b.items() if k in dict_a}
以下的方法也可以工作,并且在技术上更短,但(也许?)不太符合 Python 的风格。
dict_a = {k: dict_a[k] for k in dict_a if k in dict_b}
dict_b = {k: dict_b[k] for k in dict_b if k in dict_a}
英文:
It's not a lot better, but this uses a so-called "dictionary comprehension" to achieve it. It's effectively a for
loop in one line (but two lines here since we have two dictionaries).
dict_a = {k: v for k, v in dict_a.items() if k in dict_b}
dict_b = {k: v for k, v in dict_b.items() if k in dict_a}
This below also works, and is technically shorter but (maybe?) less Pythonic
dict_a = {k: dict_a[k] for k in dict_a if k in dict_b}
dict_b = {k: dict_b[k] for k in dict_b if k in dict_a}
答案2
得分: 3
如果您将问题转化为删除非共享键,会变得稍微容易一些:使用异或(类似集合)获取非共享键,然后删除它们出现的地方。
d1 = {'key1': 'x1', 'key2': 'y1', 'key4': 'z1'}
d2 = {'key1': 'x2', 'key3': 'y2', 'key4': 'w1'}
discard_keys = d1.keys() ^ d2.keys()
for k in discard_keys:
if k in d1:
del d1[k]
if k in d2:
del d2[k]
print(d1)
print(d2)
{'key1': 'x1', 'key4': 'z1'}
{'key1': 'x2', 'key4': 'w1'}
英文:
If you flip the problem into removing non-shared keys, it becomes a bit easier: get non-shared keys using XOR (setlike), then delete them where they occur.
d1 = {'key1': 'x1', 'key2': 'y1', 'key4': 'z1'}
d2 = {'key1': 'x2', 'key3': 'y2', 'key4': 'w1'}
discard_keys = d1.keys() ^ d2.keys()
for k in discard_keys:
if k in d1:
del d1[k]
if k in d2:
del d2[k]
print(d1)
print(d2)
{'key1': 'x1', 'key4': 'z1'}
{'key1': 'x2', 'key4': 'w1'}
答案3
得分: 2
Sure, here's the translated code:
d1 = {"key1": "x1", "key2": "y1", "key4": "z1"}
d2 = {"key1": "x2", "key3": "y2", "key4": "w1"}
common_keys = d1.keys() & d2.keys()
d1 = {k: d1[k] for k in common_keys}
d2 = {k: d2[k] for k in common_keys}
print(d1)
print(d2)
Prints:
{'key1': 'x1', 'key4': 'z1'}
{'key1': 'x2', 'key4': 'w1'}
If you need any further assistance or have questions, feel free to ask!
英文:
You can do:
d1 = {"key1": "x1", "key2": "y1", "key4": "z1"}
d2 = {"key1": "x2", "key3": "y2", "key4": "w1"}
common_keys = d1.keys() & d2.keys()
d1 = {k: d1[k] for k in common_keys}
d2 = {k: d2[k] for k in common_keys}
print(d1)
print(d2)
Prints:
{'key1': 'x1', 'key4': 'z1'}
{'key1': 'x2', 'key4': 'w1'}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论