Python 获取字符串的所有组合词

Python 获取字符串的所有组合词,python,Python,我想要字符串“我的第一个程序”的所有组合词如果您想在Python中本机执行此操作 def recurse_combinations(used, unused, dic ): if len(unused) == 0:#If unused is empty we are done dic[used]= True #Lets store the result and stop recursing return for i in range(len(u

我想要字符串“我的第一个程序”的所有组合词

如果您想在Python中本机执行此操作

def recurse_combinations(used, unused, dic ):

    if len(unused) == 0:#If unused is empty we are done
        dic[used]= True #Lets store the result and stop recursing
        return

    for i in range(len(unused)):
        #keep recursing by going through 'unused' characters and adding them to 'used'. Now lets take out the single character we are now using from 'unused'
        recurse_combinations( used + unused[i], unused[:i]+unused[i+1:], dic  )


def recurse_combinations_start( word="my first program"):
    dic = {}

    recurse_combinations( "" , word, dic)

    pprint ( dic.keys() )
    print len(dic.keys())

只需调用recurse_combinations_start()并更改要使用的单词

您是否要求使用这些字母拼写的每个单词?或者你可以用各种方法来排列这三个词?
def recurse_combinations(used, unused, dic ):

    if len(unused) == 0:#If unused is empty we are done
        dic[used]= True #Lets store the result and stop recursing
        return

    for i in range(len(unused)):
        #keep recursing by going through 'unused' characters and adding them to 'used'. Now lets take out the single character we are now using from 'unused'
        recurse_combinations( used + unused[i], unused[:i]+unused[i+1:], dic  )


def recurse_combinations_start( word="my first program"):
    dic = {}

    recurse_combinations( "" , word, dic)

    pprint ( dic.keys() )
    print len(dic.keys())