I have a list of list which is in ascending order of each list element len. For example, [[1], [1,2], [2,3], [3,4], [1,2,3], [1,3,4]]
.
I would like to get all their subset, such as
[([1],), ([1, 2],), ([2, 3],), ([3, 4],), ([1, 2, 3],), ([1, 3, 4],), ([1], [1, 2]), ([1], [2, 3]), ([1], [3, 4]), ([1], [1, 2, 3]), ([1], [1, 3, 4]), ([1, 2], [2, 3]), ([1, 2], [3, 4]), ([1, 2], [1, 2, 3]), ([1, 2], [1, 3, 4]), ([2, 3], [3, 4]), ([2, 3], [1, 2, 3]), ([2, 3], [1, 3, 4]),
…so i tried
from itertools import chain, combinations
cha=[]
end = int(len([[1], [1,2], [2,3], [3,4], [1,2,3], [1,3,4]]) // 3) + 1
for i in range(1, end):
cha.extend(list(combinations([[1], [1,2], [2,3], [3,4], [1,2,3], [1,3,4]],i)))
print(cha)
The problem is my list of list has 72 element and it would be 72C1 + 72C2 + … + 72C25. The performance is bad that it stops when 72C6.
However, there is some condition so that some combination can be skipped, which is when the total number of element in subset is more than 6. Like ([1, 2], [3, 4], [1, 2, 3])
. As my list is in len order, when it has ([1], [1, 2], [2, 3], [3, 4])
, it can skip computing ([1], [1, 2], [2, 3], [1,2,3])
and ([1], [1, 2], [2, 3], [1, 3, 4])
, and end directly. I assume a lot computation can be reduced and performance can be better. But how can I achieve this?
There is another condition that no elements in subset appears more than 2 times, such as ([1], [1, 2], [2, 3], [1, 3, 4])
(1 appears for 3 times). But this condition is not that important that violation will be eliminated later part of my code.
I think my question is a bit similar to this, I have tried filter
suggested but filter
still loop through all combination
and performance is the same.