[英]get all combinations words of a string
我想要字符串“我的第一個程序”的所有組合單詞
>>> 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')])
如果您想在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()並更改要使用的單詞即可
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.