获取一段字符串的所有排列组成结果(不区分次序)

Posted 仰望星空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取一段字符串的所有排列组成结果(不区分次序)相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys,math
import random
import pprint


b = "abcdedf"
a = list(b)


def test(a,n):
    if n==1:
        return [i for i in a]
    length = len(a)
    res = []
    for i in range(length-n+1):
        res += [a[i] + child for child in test(a[i+1:],n-1)]
    return res

def total(a):
    res = []
    for i in range(1,len(a)):
        for j in test(a,i):
            res .append(j)
    return res


print(total(a))

result:

[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘d‘, ‘f‘, ‘ab‘, ‘ac‘, ‘ad‘, ‘ae‘, ‘ad‘, ‘af‘, ‘bc‘, ‘bd‘, ‘be‘, ‘bd‘, ‘bf‘, ‘cd‘, ‘ce‘, ‘cd‘, ‘cf‘, ‘de‘, ‘dd‘, ‘df‘, ‘ed‘, ‘ef‘, ‘df‘, ‘abc‘, ‘abd‘, ‘abe‘, ‘abd‘, ‘abf‘, ‘acd‘, ‘ace‘, ‘acd‘, ‘acf‘, ‘ade‘, ‘add‘, ‘adf‘, ‘aed‘, ‘aef‘, ‘adf‘, ‘b
cd‘, ‘bce‘, ‘bcd‘, ‘bcf‘, ‘bde‘, ‘bdd‘, ‘bdf‘, ‘bed‘, ‘bef‘, ‘bdf‘, ‘cde‘, ‘cdd‘, ‘cdf‘, ‘ced‘, ‘cef‘, ‘cdf‘, ‘ded‘, ‘def‘, ‘ddf‘, ‘edf‘, ‘abcd‘, ‘abce‘, ‘abcd‘, ‘abcf‘, ‘abde‘, ‘abdd‘, ‘abdf‘, ‘abed‘, ‘abef‘, ‘abdf‘, ‘acde‘, ‘acdd‘, ‘acdf‘, ‘aced‘, ‘acef‘, ‘acdf‘, ‘ad
ed‘, ‘adef‘, ‘addf‘, ‘aedf‘, ‘bcde‘, ‘bcdd‘, ‘bcdf‘, ‘bced‘, ‘bcef‘, ‘bcdf‘, ‘bded‘, ‘bdef‘, ‘bddf‘, ‘bedf‘, ‘cded‘, ‘cdef‘, ‘cddf‘, ‘cedf‘, ‘dedf‘, ‘abcde‘, ‘abcdd‘, ‘abcdf‘, ‘abced‘, ‘abcef‘, ‘abcdf‘, ‘abded‘, ‘abdef‘, ‘abddf‘, ‘abedf‘, ‘acded‘, ‘acdef‘, ‘acddf‘, ‘ac
edf‘, ‘adedf‘, ‘bcded‘, ‘bcdef‘, ‘bcddf‘, ‘bcedf‘, ‘bdedf‘, ‘cdedf‘, ‘abcded‘, ‘abcdef‘, ‘abcddf‘, ‘abcedf‘, ‘abdedf‘, ‘acdedf‘, ‘bcdedf‘]

以上是关于获取一段字符串的所有排列组成结果(不区分次序)的主要内容,如果未能解决你的问题,请参考以下文章

2-2字典序问题

字符串的排列

获取字符串或组合的所有可能排列,包括 Java 中的重复字符

第一节 n阶行列式

树状数组求逆序数

急!!!C语言:输出两组数组中相同的数字