首页 > 技术文章 > 一个有点难的递归

echo1937 2017-10-11 21:31 原文

def permutations(param):
    if len(param) == 0:
        yield param
    else:
        for j in range(len(param)):
            param[0], param[j] = param[j], param[0]
            for k in permutations(param[1:]):
                yield [param[0]] + k


for item in permutations([1, 2, 3]):
    print(item)

http://pythontutor.com/visualize.html#

推荐阅读