python间隔删除数组中元素
时间: 2024-05-02 20:02:05 浏览: 160
可以使用 del 和切片来删除数组中的特定元素。例如,要删除数组中下标为2,3,5的元素:
```
mylist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
del mylist[2:6:2]
print(mylist)
```
输出结果为:
```
[0, 1, 3, 4, 6, 7, 8, 9]
```
此时,下标为2,3,5的元素被删除了。要保留其他元素,只需要根据需要进行切片。
相关问题
python一张二维txt数据表从第二列,第二行开始,每一列间隔96行切割一次数据,全部是零的删除的代码
假设你的二维 txt 数据表是以空格或 tab 作为分隔符的,那么你可以使用如下代码进行处理:
```python
import numpy as np
# 读取数据
data = np.loadtxt('your_data_table.txt')
# 去掉第一行和第一列
data = data[1:, 1:]
# 将每一列间隔 96 行进行切割
sliced_data = []
for i in range(0, data.shape[0], 96):
sliced_data.append(data[i:i+96, :])
# 删除全部是零的列
sliced_data = [col for col in sliced_data if not np.all(col == 0)]
# 打印处理后的结果
for col in sliced_data:
print(col)
```
其中,`loadtxt` 函数用来读取 txt 文件,`shape` 属性用来获取数组的形状,`append` 函数用来向列表中添加元素,`all` 函数用来判断数组中所有元素是否都为 True,`print` 函数用来打印结果。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)