I am copying an example from python docs.
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
How could we randomize the order of the values that we get while the result of powerset remains lazily evaluated?
EDIT: The reason I want it is that I would like to compute the sum of the derived sets and stop as soon as I find two sets that have the same sum. If I am not mistaken, the problem is NP-complete.
itertools.combinations()gives us results in a set order from the input. Given this, we can shuffle our input list to produce a random order of elements (obviously, there will be a lot less possible orders for the outcome).(This is a bit of an ugly hack – we know
shuffle(s)will always returnFalse, so we can add it as a condition to ensure it’s run for each call ofcombinations().)We pre-generate the list of lengths, so that we can shuffle that too.
It’s not perfectly random (there will still be an order – all elements of length n will be clustered together, for example, and those elements will be in an order depending on the random order of the input), but there will be a fair amount of randomness, if that is enough for you.
Example output:
I think that’s the best you can do without making it non-lazy.