I am trying to make a powerset of a list for the first item of that list in ascending order. However, I couldn't find on StackOverflow how to tackle this specific problem.
When making a powerset of the following list:
backlog = [1, 2, 3, 4, 5]
with function:
def powerset(backlog):
s = backlog
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
gets me the following result:
[(), (1,), (2,), (3,), (4,), (5,), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5), (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5), (1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5), (1, 2, 3, 4, 5)]
However, I am looking for the powerset that includes the first item of the list backlog, in this case '1', in ascending order starting with [1] and ending with [1,2,3,4,5]. How can I only filter the subsets which contain '1'?
You could filter the powerset leaving only the elements that you care about (their 1st element is the 1st element of the starting list (backlog))
But (as I specified in the comment) this is kind of inefficient as it generates lots of values (half) just to later discard them.
So, an alternative would be to generate the powerset of the initial list without its 1st element, then prepend that element to every entry in the result.
code00.py:
Output:
As seen, in this situation the latter variant performs ~4X better (time-wise).