简体   繁体   English

将两个列表与字典进行比较,并使用Python打印出不在列表中的值?

[英]Compare two list with dicts and print out value that is not in the list using Python?

I have been trying to play abit with two list that includes dicts. 我一直在尝试两个包含字典的列表。 Basically I have two list that are following: 基本上,我有以下两个列表:

listA = [{'name': 'ColorR', 'color': 'Red'}, {'name': 'ColorB', 'color': 'Blue'}, {'name': 'ColorP', 'color': 'Purple'}, {'name': 'ColorO', 'color': 'Orange'}, {'name': 'ColorW', 'color': 'White'}]
listB = [{'name': 'ColorR', 'color': 'Red'}, {'name': 'ColorP', 'color': 'Purple'}, {'name': 'ColorO', 'color': 'Orange'}, {'name': 'ColorW', 'color': 'White'}]

What I try to achieve here is that I want to compare listA with listB and print out whatever that is not in listB from listA. 我在这里想要实现的是我想将listA与listB进行比较,并从listA打印出listB中没有的内容。 In our case we don't have {'name': 'ColorB', 'color': 'Blue'} in listB meaning in that case the output would be: 在我们的例子中,我们在{'name': 'ColorB', 'color': 'Blue'}没有{'name': 'ColorB', 'color': 'Blue'} ,在这种情况下,输出为:

{'name': 'ColorB', 'color': 'Blue'}

However I did not found any similar to my problem due to I have "comma" inside a dict. 但是,由于字典中有“逗号”,因此我没有发现与我的问题类似的问题。 (maybe it is not a correct of dicts??) (也许这是不正确的命令?)

I would appreciate every sort of help to solve my issue of printing out the dicts that are not in listB. 对于解决打印出listB中没有的字典的问题的各种帮助,我将不胜感激。

you can approach this problem with a O(n^2) complexity by using in in a loop (like in this answer), but you can also rebuild listB as a set of tuples to reduce complexity to O(n) (you need to transform dictionaries into tuples so they can be added in a set ): 您可以通过in循环中使用in来解决O(n^2)复杂性的问题(例如答案),但是您也可以将listB重建为一组tuples以将复杂度降低到O(n) (您需要将字典转换为元组,以便可以将它们添加到set ):

listA = [{'name': 'ColorR', 'color': 'Red'}, {'name': 'ColorB', 'color': 'Blue'}, {'name': 'ColorP', 'color': 'Purple'}, {'name': 'ColorO', 'color': 'Orange'}, {'name': 'ColorW', 'color': 'White'}]
listB = [{'name': 'ColorR', 'color': 'Red'}, {'name': 'ColorP', 'color': 'Purple'}, {'name': 'ColorO', 'color': 'Orange'}, {'name': 'ColorW', 'color': 'White'}]

listB = {tuple(x.items()) for x in listB}

difference = [A for A in listA if tuple(A.items()) not in listB]

result: 结果:

>>> difference
[{'color': 'Blue', 'name': 'ColorB'}]

tuple(A.items()) not in listB has to build a tuple each time, but after that not in operation is O(1) tuple(A.items()) not in listB都必须构建一个tuple ,但是之后not in运行的是O(1)

It's simple. 这很简单。 Use loop to iterate each element in listA and if-statement to compare it with all elements in listB : 使用循环迭代listA每个元素,并使用if语句将其与listB所有元素进行listB

listA = [{'name': 'ColorR', 'color': 'Red'}, {'name': 'ColorB', 'color': 'Blue'}, {'name': 'ColorP', 'color': 'Purple'}, {'name': 'ColorO', 'color': 'Orange'}, {'name': 'ColorW', 'color': 'White'}]
listB = [{'name': 'ColorR', 'color': 'Red'}, {'name': 'ColorP', 'color': 'Purple'}, {'name': 'ColorO', 'color': 'Orange'}, {'name': 'ColorW', 'color': 'White'}]

for A in listA:
    if A not in listB:
        print(A)

Output: 输出:

{'name': 'ColorB', 'color': 'Blue'}
>>> listA = [{'name': 'ColorR', 'color': 'Red'}, {'name': 'ColorB', 'color': 'Blue'}, {'name': 'ColorP', 'color': 'Purple'}, {'name': 'ColorO', 'color': 'Orange'}, {'name': 'ColorW', 'color': 'White'}]
>>> listB = [{'name': 'ColorR', 'color': 'Red'}, {'name': 'ColorP', 'color': 'Purple'}, {'name': 'ColorO', 'color': 'Orange'}, {'name': 'ColorW', 'color': 'White'}]
>>> [item for item in listA if item not in listB]
[{'name': 'ColorB', 'color': 'Blue'}]
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM