python - create sequences for prefix span issue -


i trying create list containing sequences in order call prefixspan algorithm. need list in form:

 [ [["a", "b"], ["c"]], [["b", "c"], ["d"]], [["c", "d"], ["e"]]] 

where 1st 2 letters of nested list rule/sequence , single letter consequence.

i have data of form:

[[u'a',   u'b',   u'c',   u'd',   u'e',   u'f', ],  [u'a',   u'b',...]] 

applying data following logic:

   a1 =[]     in range(len(list2)):       a2 = list2[i]       j in range(len(a2)-2):         a1.append([a2[j],a2[j+1]])         a1.append([a2[j+2]]) 

and result of has following form:

 [[[u'a', u'b'],           [u'c'],           [u'd', u'e'],           [u'f'],           [u'g', u'h'],...]] 

so can't create nested 2 1 tuple sequence.

i'd list comprehension. created following code:

a = [[u'a',u'b',u'c',u'd',u'e',u'f'],[u'a2',u'b2',u'c2',u'd2',u'e2',u'f2']] print([[[v,x[i+1],[x[i+2]]] i,v in enumerate(x) if i<len(x)-2] x in a]) 

the output is:

[[[u'a', u'b', [u'c']], [u'b', u'c', [u'd']], [u'c', u'd', [u'e']], [u'd', u'e', [u'f']]], [[u'a2', u'b2', [u'c2']], [u'b2', u'c2', [u'd2']], [u'c2', u'd2', [u'e2']], [u'd2', u'e2', [u'f2']]]] 

i hope idea solve problem. further information can "nested list comprehension".


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -