Ask Your Question

Revision history [back]

The following function:

def merge_structure(lists): 
    s = sorted((a,i) for i,L in enumerate(lists) for a in L) 
    return [i for (a,i) in s]

allows to do:

sage: lists = [2,8,10], [0,3,5], [1,4,7], [6,9,11]                              
sage: merge_structure(lists)
[1, 2, 0, 1, 2, 1, 3, 2, 0, 3, 0, 3]

To translate it from indices 0,1,2,3 to a,b,c,d, do:

sage: d = dict(enumerate('abcd'))                                               
sage: [d[j] for j in merge_structure(lists)]                                              
['b', 'c', 'a', 'b', 'c', 'b', 'd', 'c', 'a', 'd', 'a', 'd']

The following function:

def merge_structure(lists): 
    s = sorted((a,i) for i,L in enumerate(lists) for a in L) 
    return [i for (a,i) in s]

allows to do:

sage: lists = [2,8,10], [0,3,5], [1,4,7], [6,9,11]                              
sage: merge_structure(lists)
[1, 2, 0, 1, 2, 1, 3, 2, 0, 3, 0, 3]

To translate it which can be translated from indices 0,1,2,3 to a,b,c,d, do:a,b,c,d as follows:

sage: d = dict(enumerate('abcd'))                                               
sage: [d[j] for j in merge_structure(lists)]                                              
['b', 'c', 'a', 'b', 'c', 'b', 'd', 'c', 'a', 'd', 'a', 'd']