Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用相同的键在字典中查找不同的值_Python_Python 3.x_Dictionary - Fatal编程技术网

在Python中使用相同的键在字典中查找不同的值

在Python中使用相同的键在字典中查找不同的值,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我有两个字典列表,它们具有相同的键,但值不同 Dict_one = {"A": ["a1", "a2", "a3"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c4"]} Dict_two = {"A": ["a1"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c5"]} 我想得到2个字典列表 一个是Dict_1中的值的字典列表,而不是Dict_2中的值 另一个是Dict_二中的值的字典列表,而不是D

我有两个字典列表,它们具有相同的键,但值不同

Dict_one = {"A": ["a1", "a2", "a3"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c4"]}

Dict_two = {"A": ["a1"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c5"]}
我想得到2个字典列表

一个是Dict_1中的值的字典列表,而不是Dict_2中的值

另一个是Dict_二中的值的字典列表,而不是Dict_一中的值

结果示例:

Result_one_two = {"A": ["a2", "a3"], "B": [], "C": ["c4"]}
Result_two_one = {"A": [], "B": [], "C": ["c5"]}
获得输出的最符合Python的方式是什么


编辑:它们始终具有相同的键

您可以在听写理解中使用
set.difference

In [29]: Result_one_two = {k: set(Dict_one[k]).difference(Dict_two[k]) for k in Dict_one}

In [30]: Result_one_two
Out[30]: {'A': {'a2', 'a3'}, 'B': set(), 'C': {'c4'}}

In [31]: Result_two_one = {k: set(Dict_two[k]).difference(Dict_one[k]) for k in Dict_one}

In [32]: Result_two_one
Out[32]: {'A': set(), 'B': set(), 'C': {'c5'}}
最好首先将值保留为
set
。在这种情况下,不需要为每个值调用集合。另外请注意,如果您使用的是Python-3.6+,因为这些版本中的字典是按插入顺序排列的,所以输出将与预期的一样,否则您应该使用
OrderedDict
来跟踪顺序。但是,如果性能不是问题,并且/或者您正在处理一个简短的数据集,您可以使用列表理解,如其他答案中所述


此外,如评论中所述,如果为了利用集合操作并保持顺序,值的顺序对您来说很重要,那么您可以使用定制的有序集合,如Raymond Hettinger在此提出的那样。

您可以在dict理解中使用
集合。差异

In [29]: Result_one_two = {k: set(Dict_one[k]).difference(Dict_two[k]) for k in Dict_one}

In [30]: Result_one_two
Out[30]: {'A': {'a2', 'a3'}, 'B': set(), 'C': {'c4'}}

In [31]: Result_two_one = {k: set(Dict_two[k]).difference(Dict_one[k]) for k in Dict_one}

In [32]: Result_two_one
Out[32]: {'A': set(), 'B': set(), 'C': {'c5'}}
最好首先将值保留为
set
。在这种情况下,不需要为每个值调用集合。另外请注意,如果您使用的是Python-3.6+,因为这些版本中的字典是按插入顺序排列的,所以输出将与预期的一样,否则您应该使用
OrderedDict
来跟踪顺序。但是,如果性能不是问题,并且/或者您正在处理一个简短的数据集,您可以使用列表理解,如其他答案中所述

此外,如评论中所述,如果为了利用集合操作并保持顺序,值的顺序对您来说很重要,那么您可以使用定制的有序集合,如Raymond Hettinger在此提出的那样。

您可以这样做:

Result_one_two = {
    k: [v for v in vals if v not in Dict_two.get(k, [])]
    for k, vals in Dict_one.items()
}
输出:

Result_one_two = {'A': ['a2', 'a3'], 'C': ['c4'], 'B': []}
Result_two_one = {'A': [], 'C': ['c5'], 'B': []}
第二点:

Result_two_one = {
    k: [v for v in vals if v not in Dict_one.get(k, [])]
    for k, vals in Dict_two.items()
}
输出:

Result_one_two = {'A': ['a2', 'a3'], 'C': ['c4'], 'B': []}
Result_two_one = {'A': [], 'C': ['c5'], 'B': []}
你可以这样做:

Result_one_two = {
    k: [v for v in vals if v not in Dict_two.get(k, [])]
    for k, vals in Dict_one.items()
}
输出:

Result_one_two = {'A': ['a2', 'a3'], 'C': ['c4'], 'B': []}
Result_two_one = {'A': [], 'C': ['c5'], 'B': []}
第二点:

Result_two_one = {
    k: [v for v in vals if v not in Dict_one.get(k, [])]
    for k, vals in Dict_two.items()
}
输出:

Result_one_two = {'A': ['a2', 'a3'], 'C': ['c4'], 'B': []}
Result_two_one = {'A': [], 'C': ['c5'], 'B': []}

我希望这段代码能帮助你

#first one:
dict_one = {"a":["a1","a2","a3"], "b": ["b1","b2"], "c":["c1","c2","c3","c4"]}
dict_two = {"a": ["a1"], "b":["b1","b2"], "c": ["c1","c2","c3","c5"]}
dict_end = {}
dict_end2 = {}
for i in dict_one:
  for j in (dict_one[i]):
    if j not in dict_two[i]:
      dict_end[i] = j

print (dict_end)
#second one goes below : 
for i in dict_two:
  for j in (dict_two[i]):
    if j not in dict_one[i]:
      dict_end2[i] = j

print (dict_end2)

我希望这段代码能帮助你

#first one:
dict_one = {"a":["a1","a2","a3"], "b": ["b1","b2"], "c":["c1","c2","c3","c4"]}
dict_two = {"a": ["a1"], "b":["b1","b2"], "c": ["c1","c2","c3","c5"]}
dict_end = {}
dict_end2 = {}
for i in dict_one:
  for j in (dict_one[i]):
    if j not in dict_two[i]:
      dict_end[i] = j

print (dict_end)
#second one goes below : 
for i in dict_two:
  for j in (dict_two[i]):
    if j not in dict_one[i]:
      dict_end2[i] = j

print (dict_end2)
类似于@kasramvd

Dict_one = {"A": ["a1", "a2", "a3"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c4"]}
Dict_two = {"A": ["a1"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c5"]}

Result_one_two = {key:list(set(Dict_one[key])-set(Dict_two[key])) for key in Dict_one.keys()}
Result_two_one = {key:list(set(Dict_two[key])-set(Dict_one[key])) for key in Dict_two.keys()}
类似于@kasramvd

Dict_one = {"A": ["a1", "a2", "a3"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c4"]}
Dict_two = {"A": ["a1"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c5"]}

Result_one_two = {key:list(set(Dict_one[key])-set(Dict_two[key])) for key in Dict_one.keys()}
Result_two_one = {key:list(set(Dict_two[key])-set(Dict_one[key])) for key in Dict_two.keys()}

您可以尝试以下几种听写理解:

Dict_one = {"A": ["a1", "a2", "a3"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c4"]}

Dict_two = {"A": ["a1"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c5"]}

# Remove this line and just loop over DictOne if using Python 3.6+
sort_keys = sorted(Dict_one)

Result_one_two = {k: [x for x in Dict_one[k] if x not in Dict_two[k]] for k in sort_keys}
Result_two_one = {k: [x for x in Dict_two[k] if x not in Dict_one[k]] for k in sort_keys}

print(Result_one_two)
print(Result_two_one)
哪些产出:

{'A': ['a2', 'a3'], 'B': [], 'C': ['c4']}
{'A': [], 'B': [], 'C': ['c5']}
注意:如果您没有使用Python 3.6+,则有必要按排序顺序或使用a遍历键,因为顺序是不保证的。否则,您可以正常循环键,并保留顺序


这还假设
Dict_one
Dict_two
具有相同的键

您可以尝试几种听写理解:

Dict_one = {"A": ["a1", "a2", "a3"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c4"]}

Dict_two = {"A": ["a1"], "B": ["b1", "b2"], "C":["c1", "c2", "c3", "c5"]}

# Remove this line and just loop over DictOne if using Python 3.6+
sort_keys = sorted(Dict_one)

Result_one_two = {k: [x for x in Dict_one[k] if x not in Dict_two[k]] for k in sort_keys}
Result_two_one = {k: [x for x in Dict_two[k] if x not in Dict_one[k]] for k in sort_keys}

print(Result_one_two)
print(Result_two_one)
哪些产出:

{'A': ['a2', 'a3'], 'B': [], 'C': ['c4']}
{'A': [], 'B': [], 'C': ['c5']}
注意:如果您没有使用Python 3.6+,则有必要按排序顺序或使用a遍历键,因为顺序是不保证的。否则,您可以正常循环键,并保留顺序



这还假设
Dict_one
Dict_two
具有相同的键

你能保证钥匙总是一样的吗?你说的“最好”是什么意思?编辑问题。到目前为止您尝试了什么,哪里出了问题?同时,已经有了答案。欢迎使用堆栈溢出。请阅读,特别是如何创建一个。您能保证密钥始终相同吗?您所说的“最佳”是什么意思?编辑问题。到目前为止您尝试了什么,哪里出了问题?同时,已经有了答案。欢迎使用堆栈溢出。请阅读,特别是如何创建订单。订单重要吗?也许最好先澄清这个糟糕的问题,然后再给出错误的答案。@PeterWood我在问题中没有发现任何关于秩序重要性的东西,但是我用一些额外的信息更新了答案。此外,如果你认为这个答案是错误的,请详细解释原因并否决我的答案。我不是说字典的键顺序。问题以列表为值,集合可以更改顺序。非常感谢您的帮助。谢谢。@Kasramvd我的时间不空闲
#happytohelp
订单重要吗?也许最好先澄清这个糟糕的问题,然后再给出错误的答案。@PeterWood我在问题中没有发现任何关于秩序重要性的东西,但是我用一些额外的信息更新了答案。此外,如果你认为这个答案是错误的,请详细解释原因并否决我的答案。我不是说字典的键顺序。问题以列表为值,集合可以更改顺序。非常感谢您的帮助。谢谢。@Kasramvd我的时间不是免费的
#happytohelp