flattening and rebuilding a simple list of lists

Esmail ebonak at hotmail.com
Mon Nov 30 08:00:48 EST 2009


Hi,

I have a list of lists. The number of sublists may vary. The sizes of
the sublists may also vary. For instance, here I have a list with 3
sublists of differing sizes.

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

This list will never be any deeper than this one level.

What I would like to do is flatten this list, rearrange the items in
the new flattened list and then recreate the list of sublists with the
new content. I would like the sublists to have the same size and order
(relative to each other) as before.

My code below seems to work, but it feels a bit "hack'ish", and I
am concerned about efficiency - so I would appreciate suggestions.

Again, the number of sublists may vary, and sizes of sublists are not
always the same (there will always be at least one sublist).

thanks!

Esmail

Here is my working code so far.

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

#!/usr/bin/env python

from random import shuffle

##################################################################
def flatten_list(parts):
     """ flatten parts, and return index vals where to split """

     line = sum(parts, [])  # join all parts into one

     idx_vals = []
     idx = 0

     for item in parts:  # keep track of lengths/index values
         idx += len(item)
         idx_vals.append(idx)

     return line, idx_vals



##################################################################
def rebuilt_lists(line, idx):
     """ rebuild list of list """

     new_list = [line[0:idx[0]]]

     for i in xrange(len(idx)-1):
         new_list.append(line[idx[i]:idx[i+1]])

     return new_list






##################################################################
def main():
     indi = [['a', 'b', 'c'], ['d', 'e'], ['f', 'g', 'h', 'i']]
     print 'indi    :', indi


     # flatten list of list
     new_indi, idx_vals = flatten_list(indi)
     print 'new_indi:', new_indi
     print 'lengths :', idx_vals
     print

     # manipulate list
     print 'shuffling new_indi'
     shuffle(new_indi)
     print 'new_indi:', new_indi
     print

     # rebuild list of lists
     new_list = rebuilt_lists(new_indi, idx_vals)
     print 'new_list:', new_list

if __name__ == '__main__':
     main()




More information about the Python-list mailing list