[Python-ideas] Implement `itertools.permutations.__getitem__` and `itertools.permutations.index`
Steven D'Aprano
steve at pearwood.info
Mon May 5 19:15:38 CEST 2014
On Mon, May 05, 2014 at 06:17:16AM -0700, Ram Rachum wrote:
> I suggest implementing:
>
> - `itertools.permutations.__getitem__`, for getting a permutation by its
> index number, and possibly also slicing, and
> - `itertools.permutations.index` for getting the index number of a given
> permutation.
>
> What do you think?
An intriguing idea.
range() objects also implement indexing, and len. But range() objects
have an obvious, unambiguous order: range(2, 6) *must* give 2, 3, 4, 5,
in that order, by the definition of range. Permutations aren't like
that. The order of the permutations is an implementation detail, not
part of the definition. If permutations provides indexing operations,
then the order becomes part of the interface. I'm not sure that's such a
good idea.
I think, rather that adding __getitem__ to permutations, I would rather
see a separate function (not iterator) which returns the nth
permutation.
--
Steven
More information about the Python-ideas
mailing list