Erik Sven Broberg Erik Sven Broberg - 1 month ago 8
Python Question

Compare two items in list to multiple lists with two values and return boolean values

I am attempting to match a single control list containing two items to multiple target lists that also contain two values each. I have achieved this in the below code, but i cannot figure out how to return multiple lists with the boolean results for each list comparison.

value = [2.55, 5.55]

a = [2.55, 3.55]
b = [4.55, 5.55]
c = [6.55, 7.68]

check = [i for i in value if i in a + b + c]
print (check)


I would like the result to be something like: (this is the desired output)..

#a = [true, false]
#b = [false, true]
#c = [false, false]


thank you

Answer

Don't know if this is what you want and if there is a mistake in your logic, but this might do the work:

value = [2.55, 5.55]

a = [2.55, 3.55]
b = [4.55, 5.55]
c = [6.55, 7.68]

# comprehension
result = [[val in lst for val in value] for lst in [a, b, c]]
# without comprehension
result = []
for lst in [a, b, c]:
    row = []
    for val in value:
        row.append(val in lst)
    result.append(row)

# if u want to asign the lists to the a, b, c, u can do instead
a, b, c = [[val in lst for val in value] for lst in [a, b, c]]
print(a, b, c)
[True, False] [False, True] [False, False]