[Python-ideas] Implement `itertools.permutations.__getitem__` and `itertools.permutations.index`
INADA Naoki
songofacandy at gmail.com
Tue May 6 00:22:56 CEST 2014
> 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 don't think the order of permutation is implementation detail.
Python implementations should follow CPython's documented order.
https://docs.python.org/3.4/library/itertools.html#itertools.permutations
> Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.
On Tue, May 6, 2014 at 2:15 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
--
INADA Naoki <songofacandy at gmail.com>
More information about the Python-ideas
mailing list