Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have two lists that returns some vector values with it's own name:

Python
list1 = [['orange', [1, 2, 3]]]
list2 = [['apple', [1, 2, 3]], ['banana', [1, 2, 3]], ['pear', [-1, 2, 3]], ['kiwi', [1, -2, 3]]]

So far I have a code that uses the list1 as a "search key" and ignoring the string on the list2 and print out the sublist that are not the same than in list1, which gives this result:
[['pear' [-1, 2, 3], ['kiwi', [-1, -2, 3]]]

This is the script:
<pre><pre lang="Python">S = set(tuple(x[1]) for x in list1)
result = [x for x in list2 if tuple(x[1]) not in S]


The current code I have working returns the correct result by ignoring the strign, but I would like to go futher and compare only the first value of the vector list, which the result should be:
[['pear' [-1, 2, 3]] only eventhough kiwi has different value but in the second item of the vector list.

What I have tried:

S = set(tuple(x[1]) for x in list1[0])
result = [x for x in list2[0] if tuple(x[1]) not in S]

but it gives a TypeError since is a tuple and not strings
Posted
Updated 11-Jan-22 1:09am

I think your code is over complicated. try the following:
Python
list1 = [['orange', [1, 2, 3]]]
list2 = [['apple', [1, 2, 3]], ['banana', [1, 2, 3]], ['pear', [-1, 2, 3]], ['kiwi', [1, -2, 3]]]
S = [x[1] for x in list1]
print('search list:', S)
# result = [x[0] for x in list2 if x[1] not in S]
result = [x[0] for x in list2 if x[1][0] != S[0][0]]
print('result', result)

However, I am not totally clear what result you are trying to obtain.
 
Share this answer
 
v2
Comments
Juan Carlos Felipe 11-Jan-22 7:00am    
Thank you for your answer, the desired result I'm looking after is returning only ['pear', [-1, 2, 3], since is the only item that in the index[0] of its list is different than the list1 index[0].
I hope this makes sense.
Richard MacCutchan 11-Jan-22 7:14am    
See my updated code. I have left the original line as a comment for comparison.
Juan Carlos Felipe 11-Jan-22 8:17am    
That's great, thank you, the only missing thing it should be to print the whole item list ['pear', [-1, 2, 3]]. Is it possible with your solution?
Richard MacCutchan 11-Jan-22 8:37am    
Yes, you can print any part, or all, of the element(s). Try
result = [x for x in list2 if x[1][0] != S[0][0]]
Juan Carlos Felipe 11-Jan-22 8:40am    
Fantastic, really appreciate your time and knowledge. Thank you :)
I found the answer thanks to the help over other site, I want to share it here just in case anyone else need it.
list1 = [['orange', [1, 2, 3]]]
list2 = [['apple', [1, 2, 3]], ['banana', [1, 2, 3]], ['pear', [-1, 2, 3]], ['kiwi', [1, -2, 3]]]

y = [x for x in list2 if x[1][0] not in [a[1][0] for a in list1]]
print(y)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900