[Python-ideas] Implement `itertools.permutations.__getitem__` and `itertools.permutations.index`
Ethan Furman
ethan at stoneleaf.us
Tue May 6 01:06:39 CEST 2014
On 05/05/2014 03:22 PM, INADA Naoki wrote:
>> 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.
What does that mean? If permutations are emitted in an order, why does the input iterable have to be ordered? What
happens if it's not?
--> list(''.join(p) for p in permutations('abc'))
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
--> list(''.join(p) for p in permutations('cab'))
['cab', 'cba', 'acb', 'abc', 'bca', 'bac']
Okay, read http://en.wikipedia.org/wiki/Lexicographical_order -- I think 'lexicographic' is not the best choice of
word... maybe positional?
--
~Ethan~
More information about the Python-ideas
mailing list