List-by-list and Python comparison
There is such a problem at the output of the function outputs the result. You need to filter the results somehow, you need to get rid of duplicates and combinations.
For example [8, 6, 4] = [4, 8, 6]
Unfortunately, I can't modify the main function, there is only the result. I understand that it is possible to check each element through loops, but maybe there is a more optimized way using libraries or some functions.
result = [[1, 2], [5, 6, 3, 2], [7, 8, 6, 3], [4, 8, 6], [1, 5, 6, 3], [2, 5, 6, 3], [7, 8, 6, 3], [4, 8, 6], [2, 5, 7, 8], [1, 5, 7, 8], [6, 3, 7, 8], [4, 6, 8], [3, 7, 4], [1, 5, 6, 7, 4], [2, 8, 1, 6, 4], [8, 6, 4]]
1 answers
Judging by the task description, the order of the elements in the nested lists in the resulting list is not so important. Therefore, we can construct a set (set
) consisting of sorted tuples:
res = set(tuple(sorted(l)) for l in result)
Result:
In [26]: res
Out[26]:
{(1, 2),
(1, 2, 4, 6, 8),
(1, 3, 5, 6),
(1, 4, 5, 6, 7),
(1, 5, 7, 8),
(2, 3, 5, 6),
(2, 5, 7, 8),
(3, 4, 7),
(3, 6, 7, 8),
(4, 6, 8)}
PS the elements of a set can only be hashed objects. The list is not a hashed object. So we had to convert the inner lists to tuples.
If the output needs a list of lists:
In [27]: [list(x) for x in res]
Out[27]:
[[1, 2],
[1, 4, 5, 6, 7],
[3, 6, 7, 8],
[1, 3, 5, 6],
[1, 5, 7, 8],
[2, 5, 7, 8],
[3, 4, 7],
[4, 6, 8],
[1, 2, 4, 6, 8],
[2, 3, 5, 6]]