如何将列表中的所有数字(Permutation)与较小的代码相乘?

时间:2016-10-15 18:52:06

标签: python python-3.x permutation

我的问题是:

    perm_list = list(range(a,b,-1))
    if len(perm_list) == 1:
       print(perm_list[0])
    elif len(perm_list) == 2:
       print(perm_list[0]* perm_list[1])
    elif len(perm_list) == 3:

如果我能做到这一点,我无法找到一种方法来缩小它 因为我会这样做到15岁,如果我写了15次,它会更大,更难写给我   如果有一个较小的是你可以请你告诉我吗?

1 个答案:

答案 0 :(得分:0)

只需使用itertools

即可
from itertools import permutations
lst = list(range(3))
result = list(permutations(lst, 2))
print(result)
=> [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
print(list(permutations(lst, 3)))
=>  [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]