我想要字符串“我的第一个程序”的所有组合单词
答案 0 :(得分: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')])
答案 1 :(得分: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()并更改您要使用的单词