嵌套循环(递归函数)求任意字符串字符排列组合

代码:

def perm(s=''):
    if len(s) <= 1:
        return [s]
    sl = []
    for i in range(len(s)):
        for j in perm(s[0:i] + s[i + 1:]):
            sl.append(s[i] + j)
    return sl


def main():
    perm_nums = perm('seddd')
    no_repeat_nums = list(set(perm_nums))  # 去重
    print('perm_nums', len(perm_nums), perm_nums)
    print('no_repeat_nums', len(no_repeat_nums), no_repeat_nums)
    pass


if __name__ == '__main__':
    main()

 

posted @ 2020-09-16 20:22  sewen  Views(227)  Comments(0Edit  收藏  举报