【发布时间】:2020-05-07 11:06:09
【问题描述】:
我有一个列表的一些排列:
>>> import itertools
>>> perms = list(itertools.permutations([0,1,2,3]))
>>> perms
[(0, 1, 2, 3), (0, 1, 3, 2), (0, 2, 1, 3), (0, 2, 3, 1), (0, 3, 1, 2), (0, 3, 2, 1), (1, 0, 2, 3), (1, 0, 3, 2), (1, 2, 0, 3), (1, 2, 3, 0), (1, 3, 0, 2), (1, 3, 2, 0), (2, 0, 1, 3), (2, 0, 3, 1), (2, 1, 0, 3), (2, 1, 3, 0), (2, 3, 0, 1), (2, 3, 1, 0), (3, 0, 1, 2), (3, 0, 2, 1), (3, 1, 0, 2), (3, 1, 2, 0), (3, 2, 0, 1), (3, 2, 1, 0)]
>>> len(perms)
24
我可以使用什么函数(不访问列表perm)来获取任意排列的索引,例如(0, 2, 3, 1) -> 3?
(您可以假设置换的元素始终是从零开始的升序整数列表。)
提示:可能涉及阶乘数系统。 https://en.wikipedia.org/wiki/Factorial_number_system
【问题讨论】:
-
您要求的是ranking 一个排列。您正在寻找的有很多实现......只需向下滚动到 Python 部分。
标签: python permutation itertools factorial