Package: grplist
grplistGroups list items using a compare function.groups=grplist([1,3,6,10,12,14,21,35],lambdaa,b:3>=abs(a-b),True)# groups => [[1, 3, 6], [10, 12, 14], [21], [35]]Table of contentsInstallExamplesReferencesInstall$ pip3 install grplistExamplesimportgrplistasglimportnumpyasnptry:importsparenLog=sparen.LogexceptExceptionase:Log=printdefshowBreak():print('\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n')deftest_1():showBreak()tests=[[1,6,3],[10,20,30,40,50],[1,3,6,10,12,14,21,35],[1,3,6,10,12,14,21,35,7,23],[1,10,20,5,15,3,7],[1,10,20,30,40,35,32,5,11,2,3,16,17,12,33,34,35,33,3,42],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18],[1,2,3,4,5,6,7,8,9,20,21,22,23,24,25,26,27,28],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]fortintests:defcmpVal(a,b):test_1.c+=1return3>=abs(a-b)test_1.c=0m=gl.grplist(t,cmpVal,True)Log(test_1.c," : ",t," -> ",m)test_1.c=0m=gl.grplist2(t,cmpVal,True)Log(test_1.c," : ",t," -> ",m)deftest_2():showBreak()a={'k0':1,'k1':3,'k2':6,'k3':10,'k4':12,'k5':14,'k6':21,'k7':35,'k8':7,'k9':23}m=gl.groupDict(a,lambdaa,b:3>=abs(a-b),False)Log(a," -> ",m)# [['k0', 'k1', 'k2', 'k3', 'k4', 'k5', 'k8'], ['k6', 'k9'], ['k7']]m=gl.groupDict(a,lambdaa,b:3>=abs(a-b),True)Log(a," -> ",m)# [['k0', 'k1', 'k2'], ['k3', 'k4', 'k5', 'k8'], ['k6', 'k9'], ['k7']]# Group lettersdeftest_3():showBreak()defanyLetters(a,b):forlina:if0<=b.find(l):returnTruereturnFalset=['on','tw','th','fo','fi','si','te','zk']Log(t," -> ",gl.grplist2(t,anyLetters,True))# Group factorsdeftest_4():showBreak()t=[3,4,5,6,7,8,9,10]Log(t," -> ",gl.grplist2(t,lambdaa,b:not(a%b),True))# Group overlapping tracksdeftest_5():showBreak()defshowTracks(t,bs,es):n=0print(' : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8')foriint:s=(' '*i[bs])+('-'*(i[es]-i[bs]+1))s='|'.join(s.replace('-','|',1).rsplit('-',1))print("%s:%s"%(n,s))n+=1t=[{'beg':2,'end':10},{'beg':20,'end':25},{'beg':4,'end':7},{'beg':30,'end':35},{'beg':8,'end':17},{'beg':22,'end':28},{'beg':33,'end':45},{'beg':1,'end':4},# {'beg': 0,  'end': 74},]print("\n--- INPUT ---")showTracks(t,'beg','end')g=gl.grplist2(t,lambdaa,b:a['beg']<=b['end']anda['end']>=b['beg'],True)i=0forting:i+=1print("\n--- GROUP%s---"%i)showTracks(t,'beg','end')defmain():test_1()test_2()test_3()test_4()test_5()if__name__=='__main__':main()Output- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    3  :  [1, 6, 3]  ->  [[1, 6, 3]]
    3  :  [1, 6, 3]  ->  [[1, 3, 6]]
    10  :  [10, 20, 30, 40, 50]  ->  [[10], [20], [30], [40], [50]]
    10  :  [10, 20, 30, 40, 50]  ->  [[10], [20], [30], [40], [50]]
    28  :  [1, 3, 6, 10, 12, 14, 21, 35]  ->  [[1, 3, 6], [10, 12, 14], [21], [35]]
    28  :  [1, 3, 6, 10, 12, 14, 21, 35]  ->  [[1, 3, 6], [10, 12, 14], [21], [35]]
    45  :  [1, 3, 6, 10, 12, 14, 21, 35, 7, 23]  ->  [[1, 3, 6, 10, 12, 14, 7], [21, 23], [35]]
    43  :  [1, 3, 6, 10, 12, 14, 21, 35, 7, 23]  ->  [[1, 3, 6, 7, 10, 12, 14], [35], [21, 23]]
    21  :  [1, 10, 20, 5, 15, 3, 7]  ->  [[20], [1, 10, 5, 3, 7], [15]]
    21  :  [1, 10, 20, 5, 15, 3, 7]  ->  [[1, 3, 5, 7, 10], [15], [20]]
    190  :  [1, 10, 20, 30, 40, 35, 32, 5, 11, 2, 3, 16, 17, 12, 33, 34, 35, 33, 3, 42]  ->  [[1, 5, 2, 3, 3], [10, 11, 12], [20, 16, 17], [30, 35, 32, 33, 34, 35, 33], [40, 42]]
    172  :  [1, 10, 20, 30, 40, 35, 32, 5, 11, 2, 3, 16, 17, 12, 33, 34, 35, 33, 3, 42]  ->  [[1, 3, 3, 2, 5], [10, 12, 11], [20, 17, 16], [30, 33, 33, 35, 34, 32, 35], [40, 42]]
    153  :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]  ->  [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]]
    57  :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]  ->  [[1, 4, 7, 10, 13, 16, 18, 17, 15, 14, 12, 11, 9, 8, 6, 5, 3, 2]]
    153  :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28]  ->  [[1, 2, 3, 4, 5, 6, 7, 8, 9], [20, 21, 22, 23, 24, 25, 26, 27, 28]]
    111  :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28]  ->  [[1, 4, 7, 9, 8, 6, 5, 3, 2], [20, 23, 26, 28, 27, 25, 24, 22, 21]]
    210  :  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  ->  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
    20  :  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  ->  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    {'k0': 1, 'k1': 3, 'k2': 6, 'k3': 10, 'k4': 12, 'k5': 14, 'k6': 21, 'k7': 35, 'k8': 7, 'k9': 23}  ->  [['k0', 'k1', 'k2', 'k3', 'k4', 'k5', 'k8'], ['k6', 'k9'], ['k7']]
    {'k0': 1, 'k1': 3, 'k2': 6, 'k3': 10, 'k4': 12, 'k5': 14, 'k6': 21, 'k7': 35, 'k8': 7, 'k9': 23}  ->  [[1, 3, 6, 10, 12, 14, 7], [21, 23], [35]]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    ['on', 'tw', 'th', 'fo', 'fi', 'si', 'te', 'zk']  ->  [['on', 'fo', 'fi', 'si'], ['tw', 'te', 'th'], ['zk']]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    [3, 4, 5, 6, 7, 8, 9, 10]  ->  [[3, 9, 6], [4, 8], [5, 10], [7]]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    --- INPUT ---
    : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8
    0:   |-------|
    1:                     |----|
    2:     |--|
    3:                               |----|
    4:         |--------|
    5:                       |-----|
    6:                                  |-----------|
    7:  |--|

    --- GROUP 1 ---
    : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8
    0:   |-------|
    1:  |--|
    2:         |--------|
    3:     |--|

    --- GROUP 2 ---
    : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8
    0:                     |----|
    1:                       |-----|

    --- GROUP 3 ---
    : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8
    0:                               |----|
    1:                                  |-----------|ReferencesPythonhttps://www.python.org/piphttps://pip.pypa.io/en/stable/