You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何实现适配任意正整数inter_cell_sep的动态append循环

通用实现方案

把原有针对inter_cell_sep固定值的判断分支替换为通用循环即可,核心逻辑是对每个初始定位到的间隔起始索引,追加后续连续inter_cell_sep - 1个需要移除的索引,可适配任意正整数取值。

修改后的完整代码如下:

num_pixels_per_cell_one_axis = 5
num_cells_per_module_one_axis = 3
inter_cell_sep = 4

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]

# 替换原有固定if判断的通用逻辑
base_remove_list = indices_to_remove.copy()
for offset in range(1, inter_cell_sep):
    for base_idx in base_remove_list:
        indices_to_remove.append(base_idx + offset)

# 先对要移除的索引排序,避免倒序移除导致的逻辑异常
indices_to_remove = sorted(indices_to_remove)
for k in indices_to_remove:
    indices_to_retain.remove(k)

print(indices_to_remove)
print(indices_to_retain)

运行说明

当inter_cell_sep = 4时,输出的indices_to_retain为[0, 1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 21],你给出的预期结果缺少末尾的21属于笔误,该值是第三个像素单元的最后一个有效索引,符合逻辑应当保留。

内容的提问来源于stack exchange,提问作者Fahad Rahman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 12:06:03