I have two lists that returns some vector values with it’s own name:
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:
S = set(tuple(x[1]) for x in list1) result = [x for x in list2 if tuple(x[1]) not in S]
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.
Advertisement
Answer
You can store the first value of list1
into another list and use filter on list2
list1 = [['orange', [1, 2, 3]]] list2 = [['apple', [1, 2, 3]], ['banana', [1, 2, 3]], ['pear', [-1, 2, 3]], ['kiwi', [1, -2, 3]]] a = [l[1][0] for l in list1] # a is [1] (first value of vector list) list(filter(lambda x: x[1][0] not in a, list2)) # This will return expected out put i.e: [['pear', [-1, 2, 3]]]