2010-08-16 2 views

ответ

8
>>> lst = "my first program".split() 
>>> set(itertools.permutations(lst)) 

set([('first', 'my', 'program'), 
    ('first', 'program', 'my'), 
    ('my', 'first', 'program'), 
    ('my', 'program', 'first'), 
    ('program', 'first', 'my'), 
    ('program', 'my', 'first')]) 
0

Если вы хотели бы сделать это изначально в 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() и измените слово, которое вы хотите использовать

Смежные вопросы