-2

There are two phones, phoneA and phoneB, how to how to find complement in phoneB but not in phoneA, one 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”]

I write code like this by python:

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

but the code is wrong, anyone has better idea?

bereal
  • 32,519
  • 6
  • 58
  • 104
  • 2
    Could you please clarify what's wrong with your code? – bereal Apr 10 '22 at 08:16
  • 1
    Why do you have two lists assigned to each phone variable? – mbostic Apr 10 '22 at 08:17
  • Ah I didn't see those second lists due to the line lengths :) So both `phoneA` and `phoneB` are tuples. – bereal Apr 10 '22 at 08:18
  • @bereal I don't understand, can you edit again so that it will be correct? :) – mbostic Apr 10 '22 at 08:20
  • @bereal I see, what confused me was wrong apostrophe used in strings. – mbostic Apr 10 '22 at 08:26
  • 1
    @mbostic I only added the indentation, so the tuples were there in the beginning. – bereal Apr 10 '22 at 08:27
  • @hangkai wang How do you want to treat the fact that there are two lists for each phone? – mbostic Apr 10 '22 at 08:27
  • What do you mean by compliment? Are you after things in b that are not in a? – Freddy Mcloughlan Apr 10 '22 at 08:29
  • The problem is to input two parameters of mobile phone A and B, which can be in the form of list or array, and then find out the parameters in mobile phone B but not in mobile phone A. I wrote the above code by myself, but there is an error in the submission, I don't know where the error is. The parameters are shown above. – hangkai wang Apr 10 '22 at 08:39

1 Answers1

0

You need to add the 2 lists together, then check them:

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

Or if you have multiple sublists, this will also work:

# 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]]
Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29