将范围列表转换为单个列表

Transform a list of ranges into a single list

我有一个数据框,其中有一些点可以标记另一个数据集。 我正在从开始标记和停止标记创建一个范围,我想将其转换为单个列表或 numpy 数组。

我有以下内容:

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

这将返回一个数组列表:

[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]),
...]

如何修改或转换输出以具有单个列表或 NumPy 数组,如下所示:

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,...])

就这样:

flatarray = np.concatenate(list_of_arrays)

concatenate 将两个或多个 array 组合成一个新的 array;您不会一次只做一个 array(它会创建一个 Schlemiel the Painter 的算法),但是一旦您掌握了所有这些,这是将它们组合起来的有效方法。