gpt4 book ai didi

python - 索引错误,如果列有值,则从数组中删除行

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:32 26 4
gpt4 key购买 nike

我有一个包含四列的数组“x”。

对于每一行,如果第 4 列的值为 1,那么我想删除整行:

x = np.array([[1,2,3,0],[11,2,3,24],[1,22,3,1],[1,22,3,1], [5,6,7,8], [9,10,11,1]])
for i in range(0,len(x)):
if x[i][4]==0:
x=np.delete(x, i,0)

我收到以下错误:

Traceback (most recent call last):
File "", line 2, in
if x[i][4]==0:
IndexError: index out of bounds

最佳答案

您试图用 [4] 引用第四列,但由于它是从零开始的,所以它实际上是 [3]

关于python - 索引错误,如果列有值,则从数组中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18858396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com