2010-08-16 3 views

Répondre

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

Si vous souhaitez faire nativement en 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()) 

simplement appelez cela recurse_combinations_start() et changez le mot que vous voulez utiliser

Questions connexes