假设有一个数组,例如 x = ['A','I','R']
,我想要输出为
[['A','I','R'],['A','I'],['I','R'],['A'],['I'],['R']]
我不想要的输出是:
[['A','I','R'],['A','I'],['I','R'],['A','R'],['A'],['I'],['R']] # extra ['A','R'] which is not in sequence .
以下是会产生我不想要的输出的代码:
letter_list = [a for a in str]
all_word = []
for i in xrange(0,len(letter_list)):
all_word = all_word + (map(list, itertools.combinations(letter_list,i))) # dont use append. gives wrong result.
all_word = filter(None,all_word) # remove empty combination
all_word = all_word + [letter_list] # add original list
我的意思是我只想要序列的组合。是否有使用 itertools
的方法,还是我应该编写自定义函数?