如何在两个列表中找到补码

how to find complement in two lists

有两个phone,phoneA和phoneB,如何在phoneB中找到补码,而在phoneA中找不到补码,一个ex;

phoneA =  ["long lasting battery”, ”clear display”, ”great camera”, ”storage space”], [“clear display”, ”long lasting battery”, ”great camera”, ”warp-speed word processing”]

phoneB =  ["long lasting battery”, ”clear display”, ”great camera”, ”storage space”], [“clear display”, ”long lasting battery”, ”great camera”, ”warp-speed word processing”, “great sound”]

我写这样的代码 python:

d = [x for x in phoneB if x not in phoneA]
print(d)

但是代码是错误的,谁有更好的主意?

您需要将 2 个列表加在一起,然后检查它们:

d = [x for x in phoneB[0]+phoneB[1] if x not in phoneA[0]+phoneA[1]]
>>> d
['great sound']

或者如果您有多个子列表,这也可以:

# This joins all the elements in sublists into one large list
d = [x for x in [j for i in phoneB for j in i]
     if x not in [j for i in phoneA for j in i]]