I have a vector of numbers in numpy
. For example :
a = [0,1,2,3]
and I have a list of vectors:
b = [[0,1] , [0,2], [0,3], [1,2], [1,3]]
what is the best solution in Python that allows me to find the missing couple of elements in b? In this case it would be:
c = [2,3]
I would add that it is not important the order of the elements, I only need the missing couple.
A first try
I tried using the capabilities of the sets
, that through symmetric_difference
it gives the missing elements of a in an element of b:
for j in range(len(b)):
list(set(b[j]).symmetric_difference(a)
But I do not manage to figure out the end of the algorithm or an alternative way.
CodePudding user response:
You can first create all combinations with itertools.combinations
then use difference
in set
like below: (this works for multi elements)
>>> import itertools
>>> a = [0,1,2,3]
>>> b = [[0,1] , [0,2], [0,3], [1,3]]
>>> c = itertools.combinations(a,2)
>>> res = set(c).difference(map(tuple, b))
>>> list(map(list, res))
[[2, 3], [1, 2]]
# for more explanation
>>> set(itertools.combinations(a,2))
{(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)}