New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extending itertools.combinations #49298
Comments
The function itertools.combinations might benefit from making With that change one could call combinations(sequence, [2, 3]) in The change would be backwards compatible as it would check for |
Do you have compelling use cases? Is it worth what it takes to preserve the relation with permuations() Do we care the length of the output is no longer predictable by a simple Do we care that the variable length output prevents uses in for-loops for a, b, c in combinations('ABCDEF', [2,3]):
. . . |
It would also be nice to have a __len__ method on both permutations and len(permutations(sequence)) could return the number of permutations Also, is there a way to calculate combinations with repetitions1? |
Guido disallowed returning a len method on iterators. He expected The recipes section of the itertools docs has code for |
I've seen requests for combinations with replacement come up on c.l.p. a -1 for sequences in the second argument of combinations(); it doesn't |
I'm afraid I don't have any real-world use cases. Originally, I assumed max((comb for comb in all_combinations(zip(weights, values))
if sum(map(itemgetter(0), comb)) < LIM),
key=lambda comb: sum(map(itemgetter(1), comb))) But unfortunately, this is far from being 'compelling'. Regarding other issues you raised: I think it would be pretty clear for |
Now *this* sounds more useful to me: an itertool that generates *all* subsets of a list. It's def subsets(iterable):
l = list(iterable)
for selector in itertools.product([False, True], repeat=len(l)):
yield [element for indicator, element in zip(selector, l) if indicator] |
There is already a powerset() recipe in the docs. It was contributed by |
So there is. Apologies---I'll try to read more carefully next time. |
Am rejecting the OP's request for the reasons cited. Also rejecting the request for a __len__ method on permutations. Ezio or Mark, please open another feature request for Thank you to all the posters on this one. |
FWIW, I added combinations_with_replacement() in r69001 and r69004 . |
combinations_with_replacement addition looks good. Thank you. I wonder whether the second argument should be constrained to be an >>> list(combinations_with_replacement(range(5), -0.3))
[()] (same behaviour with combinations and permutations). Should I open a |
Glad you like it. Will meditate for a bit on the floating point input. My first thought |
I only just noticed the DeprecationWarning that's already happening for |
Correction to earlier statement. It should have read: Guido disallowed returning a len method on iterators. He expected |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: