python - Splitting a list into groups determined by grouping of another list -


say have 2 lists

a = [true, true, true, false, false, true, false] b = [1, 2, 1, 3, 2, 1, 0] 

i can group 1 list similar items:

import itertools b =  [list(g) k, g in itertools.groupby(a)] >>> [[true, true, true], [false, false], [true], [false]]  

is there convenient way apply operation other list give:

>>> [[1,2,1], [3,2], [1], [0]] 

this works there nicer:?

new_list = [] index = 0 in b:     length = len(i)     new_list.append(y[index:index+length])     index += length 

thanks

you can groupby on enumerate(a):

>>> itertools import groupby >>> operator import itemgetter >>> indices = ((x[0] x in g) _, g in groupby(enumerate(a), key=itemgetter(1))) >>> [[b[x] x in lst] lst in indices] [[1, 2, 1], [3, 2], [1], [0]] 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -