This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [request feature] Itertools extended combinations to limited number of repetition
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Dennis Sweeney, latot, rhettinger, tim.peters
Priority: normal Keywords:

Created on 2021-05-20 20:26 by latot, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg394061 - (view) Author: latot (latot) Date: 2021-05-20 20:26
Hi, some time ago I was looking for a function in itertools, actually, the combination functions allow us to have or not repetitions of element, have in consideration that if we allow repetitions there can be any amount of them, no way to limit the number of repetitions.

Here I asked in stackoverflow about this if there is a lib or option, in the end we wasn't able to found any about this and there was an answer with the solution.

I think this solution worth to be added to itertools, conceptually, the actual 2 functions of combinations are particular cases of this function.

https://stackoverflow.com/questions/67559105/combinatory-with-n-picked-elements-and-limited-repetitions-in-python/67559625

Well, there can be a extension of this too, where we can say, "how many times every particular element can be repeated".

Both of this things world be useful.

Thx.
msg394079 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-05-20 23:37
How is proposed_function("abcd", max_repeat=3) any different from what you can currently spell as combinations("aaabbbcccddd") ? Or, more generally,

def proposed_function(it, repeats)
    repeated = chain.from_iterable([x] * repeat for x in it)
    return combinations(repeated)

This can easily generalize to specifying the max count of each item.
msg394095 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2021-05-21 02:00
Dennis, combinations("aaabbbcccddd") isn't a valid call - the function requires a "how many?" argument too. If, e.g., we were asking for groups of 4, then combinations("aaabbbcccddd", 4) generates the 4-tuple ('a', 'b', 'c', 'd') 81 (3**4) times, while the OP presumably only wants to get it once.

OTOH, combinations_with_replacement("abcd", 4) can generate tuples with more than 3 repetitions of a given element.

The linked StackOverflow entry gives an efficient "real solution", but I agree with its author's final comment: "It is much too specialized for itertools". Indeed, it seems too obscure and special-purpose to me to even qualify as a reasonable candidate for an itertools doc "recipe".
msg394134 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-05-21 17:40
> it seems too obscure and special-purpose to me to even
> qualify as a reasonable candidate for an itertools doc "recipe"

My thoughts are the same.

@latot Thank you for the suggestion but it doesn't make sense for the standard library.  We respectfully decline.
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88363
2021-05-21 17:41:14rhettingersetassignee: rhettinger
stage: resolved ->
type: enhancement
versions: + Python 3.11
2021-05-21 17:40:07rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg394134

stage: resolved
2021-05-21 02:00:27tim.peterssetnosy: + rhettinger
2021-05-21 02:00:05tim.peterssetnosy: + tim.peters
messages: + msg394095
2021-05-20 23:37:49Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg394079
2021-05-20 20:26:22latotcreate