Python:获取list的顺序元素的所有组合

Python: Get all combinations of sequential elements of list

给定一个数组说 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 还是我应该编写自定义函数?

不要试图变得如此神奇:两个循环就可以完成你想要的;一个超过可能的序列开始,内部超过可能的序列长度:

x = "AIR" # strings are iterables/sequences, too!
all_words = []
for begin in xrange(len(x)):
    for length in xrange(1,len(x) - begin+1):
        all_words.append(x[begin:begin+length])

使用列表理解:

letters=['A', 'I', 'R']
[letters[start:end+1] 
 for start in xrange(len(letters)) 
 for end in xrange(start, len(letters))]

[['A'], ['A', 'I'], ['A', 'I', 'R'], ['I'], ['I', 'R'], ['R']]

如果您建议的顺序很重要(从最长到最短,并且起始位置的长度相同),您可以改为:

[letters[start:start+l+1]
 for l in range(len(letters))[::-1]
 for start in xrange(len(letters)-l)]

[['A', 'I', 'R'], ['A', 'I'], ['I', 'R'], ['A'], ['I'], ['R']]

只是为了解决 Holroy 的评论。如果不使用列表理解,而是使用生成器表达式(只需将外部 [] 替换为 ()),您将获得更少的代码内存需求。但在这种情况下,您必须注意不要多次使用结果,或者不要尝试对结果使用列表方法(例如 len 或删除元素)。

尝试使用yield:

x = ['A','I','R']

def groupme(x):
    s = tuple(x)
    for size in range(1, len(s) + 1):
        for index in range(len(s) + 1 - size):
            yield list(x[index:index + size])

list(groupme(x))

>>> [['A'], ['I'], ['R'], ['A', 'I'], ['I', 'R'], ['A', 'I', 'R']]

是的,您可以使用 itertools:

>>> x = ['A', 'I', 'R']
>>> xs = [x[i:j] for i, j in itertools.combinations(range(len(x)+1), 2)]
>>> xs
[['A'], ['A', 'I'], ['A', 'I', 'R'], ['I'], ['I', 'R'], ['R']]
>>> sorted(xs, key=len, reverse=True)
[['A', 'I', 'R'], ['A', 'I'], ['I', 'R'], ['A'], ['I'], ['R']]

来源:answer by hochl