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

Steven D'Aprano steve at pearwood.info
Tue May 6 04:39:02 CEST 2014


On Tue, May 06, 2014 at 07:22:56AM +0900, INADA Naoki wrote:

> 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

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.

> > Permutations are emitted in lexicographic sort order. So, if the 
> > input iterable is sorted, the permutation tuples will be produced in 
> > sorted order.

I think I know what the docs are trying to say, but I'm not sure if they 
are quite saying it correctly. If the permutations are emitted in 
"lexicographic sort order", that implies that they are sortable, but 
that's not necessarily the case:

py> 4j > 2j
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: no ordering relation is defined for complex numbers
py> list(itertools.permutations([4j, 2j]))
[(4j, 2j), (2j, 4j)]

I think that just removing the word "sort" is sufficient: "Permutations 
are emitted in lexicographic order" is meaningful, and correct, even 
when the elements are not sortable.



-- 
Steven


More information about the Python-ideas mailing list