Home > Software design >  Transform a list of ranges into a single list
Transform a list of ranges into a single list

Time:05-07

I have a data frame that have some points to mark another dataset. I'm creating a range from the starting mark and the stopping mark that I want to transform into a single list or numpy array.

I have the following:

list(map(lambda limits : np.arange(limits[1] - limits[0]-1, -1, -1),
         zip(df_cycles['Start_point'], df_cycles['Stop_point']))
    )

This is returning a list of arrays:

[array([1155, 1154, 1153, ...,    2,    1,    0]),
 array([71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55,
        54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38,
        37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
        20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,
         3,  2,  1,  0]),
...]

How can I modify or transform the output to have a single list or NumPy array like this:

array([1155, 1154, 1153, ...,    2,    1,    0, 71, 70, 69, 68, 67, 66, 65,
        64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 
        47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
        30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 
        13, 12, 11, 10,  9,  8,  7,  6,  5,  4,3,  2,  1,  0,...])

CodePudding user response:

Just do:

flatarray = np.concatenate(list_of_arrays)

concatenate puts together two or more arrays into a single new array; you don't to do it a single array at a time (it creates a Schlemiel the Painter's algorithm), but once you've got them all, it's an efficient way to combine them.

  • Related