Home > Enterprise >  subsequencing of list of sequence
subsequencing of list of sequence

Time:10-17

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.

CodePudding user response:

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']]

CodePudding user response:

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']]
  • Related