Python list sorting conundrum -


i have unusual list sorting problem i'm struggling find concise , efficient way of solving. problem follows: have list called indexes each of items in list corresponding index second list data. example first number in index 0 corresponds first item in list data 1 , 3 in index corresponds 6 in list data because 6 3rd item in list data [including 0th item]..

now want organise list data that grouped between indexes below:

indexes = [ 0, 3, 6, 8, 12, 13]  data = [1, 2, 5, 6, 11, 13, 15, 23, 35, 36, 38, 41, 46] 

solution:

organised_data = [ [1,2,5], [6,11,13], [15,23], [35,36,38,41], [46] ] 

i've been @ ages.

you can use slicing between consecutive indexes in list comprehension

>>> [data[indexes[i]:indexes[i+1]] in range(len(indexes)-1)] [[1, 2, 5], [6, 11, 13], [15, 23], [35, 36, 38, 41], [46]] 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -