全排列

# coding=utf-8
import itertools

for i in itertools.permutations('abcd',4):
    print (''.join(i))

'''def perm(l):
    if(len(l)<=1):
        return [l]
    r=[]
    for i in range(len(l)):
        s=l[:i]+l[i+1:]
        p=perm(s)
        for x in p:
            r.append(l[i:i+1]+x)
    return r
    '''




COUNT = 0


def perm(n, begin, end):
    global COUNT
    if begin >= end:
        print(n)
        COUNT += 1
    else:
        i = begin
        for num in range(begin, end):
            n[num], n[i] = n[i], n[num]
            perm(n, begin + 1, end)
            n[num], n[i] = n[i], n[num]

if __name__ == '__main__':

    n = [1, 2, 3, 4]
    perm(n, 0, len(n))
    print(COUNT)

你可能感兴趣的:(python学习)