如何使用numpy重复跳过数组中一定数量的项目后提取新数组
How to extract a new array after skipping a certain number of items in the array repeatly using numpy
num_pixels_per_cell_one_axis = 4
num_cells_per_module_one_axis = 4
inter_cell_sep = 2
max_items_in_list = num_cells_per_module_one_axis * num_pixels_per_cell_one_axis + (num_cells_per_module_one_axis-1) * inter_cell_sep
print(max_items_in_list)
indices_to_retain = list(range(max_items_in_list))
indices_to_remove = indices_to_retain[num_pixels_per_cell_one_axis :: num_pixels_per_cell_one_axis + inter_cell_sep]
我想要得到的结果是列表 indices_to_retain =[0,1,2,3,6,7,8,9,12,13,14,15,18,19 ,20,21]
IIUC 你想保留 4 项,然后跳过 2 项吗?
您可以使用:
keep, skip = 4,2
indices_to_retain = [i for i in range(max_items_in_list) if i%(skip+keep)<keep]
输出:
>>> indices_to_retain
[0, 1, 2, 3, 6, 7, 8, 9, 12, 13, 14, 15, 18, 19, 20, 21]
注意。有趣的是,我 answered to a very similar question 就在几个小时前
使用 numpy:
indices_to_retain = np.arange(max_items_in_list)
indices_to_retain = indices_to_retain[indices_to_retain%(skip+keep)<keep]
num_pixels_per_cell_one_axis = 4
num_cells_per_module_one_axis = 4
inter_cell_sep = 2
max_items_in_list = num_cells_per_module_one_axis * num_pixels_per_cell_one_axis + (num_cells_per_module_one_axis-1) * inter_cell_sep
print(max_items_in_list)
indices_to_retain = list(range(max_items_in_list))
indices_to_remove = indices_to_retain[num_pixels_per_cell_one_axis :: num_pixels_per_cell_one_axis + inter_cell_sep]
我想要得到的结果是列表 indices_to_retain =[0,1,2,3,6,7,8,9,12,13,14,15,18,19 ,20,21]
IIUC 你想保留 4 项,然后跳过 2 项吗?
您可以使用:
keep, skip = 4,2
indices_to_retain = [i for i in range(max_items_in_list) if i%(skip+keep)<keep]
输出:
>>> indices_to_retain
[0, 1, 2, 3, 6, 7, 8, 9, 12, 13, 14, 15, 18, 19, 20, 21]
注意。有趣的是,我 answered to a very similar question 就在几个小时前
使用 numpy:
indices_to_retain = np.arange(max_items_in_list)
indices_to_retain = indices_to_retain[indices_to_retain%(skip+keep)<keep]