[Python-ideas] Implement `itertools.permutations.__getitem__` and `itertools.permutations.index`

Tal Einat taleinat at gmail.com
Tue May 6 17:40:53 CEST 2014


On Tue, May 6, 2014 at 3:45 PM, Paul Moore <p.f.moore at gmail.com> wrote:
> On 6 May 2014 10:35, Tal Einat <taleinat at gmail.com> wrote:
>>> Hmmm. Well, since the order of permutations is documented, I suppose my
>>> objection is answered. In that case, it becomes a question of whether or
>>> not there is an easy way to generate the Nth permutation without having
>>> to iterate through the previous N-1 permutations.
>>
>> Yes, it is possible using factorial decomposition of N.
>>
>> See, for an example: http://stackoverflow.com/a/7919887/40076
>
> For large N, this is much slower than itertools.permutations when you
> only want the first few entries.

If someone just wants the first few entries, they probably aren't
worried about it being super fast. And if they were, they could just
iterate to get the first permutations.

As for getting anything past the first few permutations (e.g. an
arbitrary one), factorial decomposition would be faster by several
orders of magnitude than iterating from the beginning. For relatively
large permutations, iterating from the beginning could be unfeasible,
while factorial decomposition would still take far less than a second.

The real question IMO is if this is useful enough to bother including
in the stdlib. For example, I don't think it would pass the "potential
uses in the stdlib" test. Perhaps Ram (the OP) has some actual
use-cases for this?

- Tal


More information about the Python-ideas mailing list