使用时计算排列时出现意外结果itertools.排列

2024-03-28 20:34:25 发布

您现在位置:Python中文网/ 问答频道 /正文

给定的字符串是'0123456789'。 我想生成它的百万分之一排列(1000000)。在

我想先试试itertools.permutations

In [85]: import itertools

In [86]: a = list(itertools.permutations('0123456789'))

In [87]: a[1]
Out[87]: ('0', '1', '2', '3', '4', '5', '6', '7', '9', '8')

In [88]: a[1000000]
Out[88]: ('2', '7', '8', '3', '9', '1', '5', '6', '0', '4')

但是,如果我运行以下代码:

^{pr2}$

答案来自itertools.排列与上面的psuedo代码不匹配。在

[2783915604] from itertools
[2783915460] from the snippet above

第二个答案是正确答案。有人能解释一下为什么第一个过程没有得到正确的结果吗?在


Tags: the字符串答案代码infromimportout