Heap's algorithm permutation generator

前端 未结 2 1908
日久生厌
日久生厌 2020-12-07 21:38

I need to iterate over permutations of a tuple of integers. The order has to be generated by swapping a pair of elements at each step.

I found the Wikipedia article

2条回答
  •  伪装坚强ぢ
    2020-12-07 22:15

    The easiest way to get the permutations of a list would be the permutations function, in the itertools module. So, if the algorithm is not binding, I would go with this:

    from itertools import permutations
    
    a = [1,2,3,4]
    for item in permutations(a):
        print item
    

提交回复
热议问题