subsequencing of list of sequence

Question:

I have following sequence of data:

['A',
 'A',
 'A',
 'A',
 'A',
 'A',
 'A',
 'D',
 'D',
 'D',
 'D',
 'D',
 'D',
 'A',
 'A',
 'A',
 'A',
 'A',
 'D',
 'D',
 'D',
 'D',
 'D',
 'D']

How would I be able to create subsequence (list of list) as an example:

[['A',
 'A',
 'A',
 'A',
 'A',
 'A',
 'A'], 
 ['D',
 'D',
 'D',
 'D',
 'D',
 'D'],
 ['A',
 'A',
 'A',
 'A',
 'A'], ['D',
 'D',
 'D',
 'D', 'D', 'D']]

That is I want to create a sublist which accumulates the first encountered value (eg ‘A’ or ‘D’ and append that to sublist and continue until it arrives at a different alphabet. The second list contains the sequence of the letters that were different than the first sequence and appends as sublist. The process continues until the last element of the main list.

Asked By: lpt

||

Answers:

itertools.groupby is a good tool for this kind of tasks:

from itertools import groupby

lst = ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'D', 'D', 'D', 'D', 'D', 'D', 'A', 'A', 'A', 'A', 'A', 'D', 'D', 'D', 'D', 'D', 'D']

output = [list(g) for _, g in groupby(lst)]
print(output)
# [['A', 'A', 'A', 'A', 'A', 'A', 'A'], ['D', 'D', 'D', 'D', 'D', 'D'], ['A', 'A', 'A', 'A', 'A'], ['D', 'D', 'D', 'D', 'D', 'D']]
Answered By: j1-lee

Solution:

import itertools
    
lis = ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'D', 'D', 'D', 'D', 'D', 'D', 'A', 'A', 'A', 'A', 'A', 'D', 'D', 'D', 'D', 'D', 'D']

print([list(x[1]) for x in itertools.groupby(lis)])

Output:

[['A', 'A', 'A', 'A', 'A', 'A', 'A'], ['D', 'D', 'D', 'D', 'D', 'D'], ['A', 'A', 'A', 'A', 'A'], ['D', 'D', 'D', 'D', 'D', 'D']]
Answered By: Bhargav
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.