Python supports sets (more about sets). For three lists it will be:
A = [1, 2, 3, 4]
B = [2, 3, 5, 6]
C = [3, 4, 5, 7]
As = set(A)
Bs = set(B)
Cs = set(C)
print((As ^ Bs ^ Cs) ^ (As & Bs & Cs))
For list of lists (this is wrong - all it does is XORing all sets, ANDing all sets and than XORing this two results - correct solution below):
import functools
def do_xor(s1, s2):
return s1 ^ s2
def do_and(s1, s2):
return s1 & s2
def do_task(list_of_lists):
list_of_sets = list(map(set, list_of_lists))
xors = functools.reduce(do_xor, list_of_sets)
ands = functools.reduce(do_and, list_of_sets)
return xors ^ ands
A = [1, 2, 3, 4]
B = [2, 3, 5, 6]
C = [3, 4, 5, 7]
D=[A, B, C]
print(do_task(D))
Correct solution:
import functools
def do_or(s1, s2):
return s1 | s2
def do_task2(list_of_lists):
list_of_sets = list(map(set, list_of_lists))
list_of_intersects = [X & Y for X in list_of_sets for Y in list_of_sets if X is not Y]
intersects = functools.reduce(do_or, list_of_intersects)
ors = functools.reduce(do_or, list_of_sets)
return ors - intersects
lol33 = [
[1, 2],
[3, 2],
[3],
[3, 2, 4]
]
print(do_task2(lol33)) # {1, 4}
n
lists? – Reinert