首页 > 解决方案 > Python slice all except the second to last row in an array

问题描述

I know how to slice an array with all except a row at the beggining or end of an array but not an array with just one row in the middle (like the second to the last row) missing. How would I do this?

标签: python-3.xslice

解决方案


要使用切片从中间删除一个项目,您几乎总是需要连接两个不同的切片,一个用于之前的项目,一个用于之后的项目。如果n是您要删除的项目的索引,您通常可以这样做

new_list = old_list[:n] + old_list[n+1:]

推荐阅读