【发布时间】:2015-10-01 08:40:45
【问题描述】:
给定一个数组说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 还是我应该编写自定义函数?
【问题讨论】:
-
此处可能存在重复:Substrings of a string
-
请注意在 Code Review SE 的 my answer 到您的 follow up question 中解释的指数内存使用情况。对于长度为 1000 个字符的文本,这样拆分时需要 167167000 个字符,内存占用为 1.25GB。