简体   繁体   English

如何删除 numpy 数组中的元素并在 python 及其索引中使用 for 循环

[英]how can i delete an element in numpy array and use a for loop in python and its index

This is what I made and it doesn't work, I made a for loop and I use it to get the index and use it in another thing why doesn't it work or can I found another method to delete the element and use the index of it.这是我所做的,但它不起作用,我做了一个 for 循环,我用它来获取索引并在另一件事中使用它为什么它不起作用或者我可以找到另一种方法来删除元素并使用它的索引。

Here is some of my code这是我的一些代码

X1_train, X1_test, y1_train, y1_test = train_test_split(EclipseFeautres, EclipseClass, test_size=0.3, random_state=0)
E_critical_class=y1_train.copy()
E_critical_class = E_critical_class[E_critical_class != 1]
for x in range(len(E_critical_class)):
if(E_critical_class[x]==1):
    E=np.delete(E_critical_class,x)

Your task is something like filtering of an array.您的任务类似于过滤数组。 You want to drop all elements == 1 .您想删除所有元素== 1

Assume that the source array ( arr ) contains:假设源数组 ( arr ) 包含:

array([0, 1, 2, 3, 4, 1, 0, 3, 7, 1])

so it contains 3 elements == 1 (to be dropped).所以它包含 3 个元素== 1 (要删除)。

A much simpler way to do it is to use boolean indexing and save the result back to the original variable:一个更简单的方法是使用boolean 索引并将结果保存回原始变量:

arr = arr[arr != 1]

The result is:结果是:

array([0, 2, 3, 4, 0, 3, 7])

as you wish - with all one s dropped.如你所愿 - 所有都掉了。

#dizi icinde olan ve kendini tekrarlayan sayiyi delete etme!!!!!!
#to delete the repeated element in the numpy array
import numpy as np
a = np.array([10, 0, 0, 20, 0, 30, 40, 50, 0, 60, 70, 80, 90, 100,0])
print("Original array:")
print(a)
index=np.zeros(0)
print("index=",index)
for i in range(len(a)):
    if a[i]==0:
        index=np.append(index, i)
print("index=",index)
new_a=np.delete(a,index)
print("new_a=",new_a)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在不使用python 2.7中的for循环的情况下使用numpy数组的索引 - How can I use the index of an numpy array in place without for loop in python 2.7 如何使用python将所有索引值的总和加到numpy ndarray的每个元素中? - How can I add to each element of a numpy ndarray the sum of all its index values with python ? 如何从来自 numpy 数组的循环中获取元素的索引? - How can I get the index of an element from a loop, sourced from a numpy array? 如何获取里面有列表的python numpy数组中任何元素的索引? - How can I get Index of any element in python numpy array that has list inside? 如何在不使用嵌套 for 循环的情况下根据其索引值划分 2d numpy 数组中的每个元素? - How to divide each element in a 2d numpy array in relation to its index value without the use of nested for loops? Python with numpy:如何根据特定索引从二维数组的每一行中删除一个元素 - Python with numpy: How to delete an element from each row of a 2-D array according to a specific index 有没有办法用索引替换 numpy 数组中的元素 - Is there way to replace an element in a numpy array with its index 在Python / numpy中,如何根据数组的第一列(即索引)解散/拆分数组? - In Python / numpy, how do I unravel / split an array according to its first column, which is an index? 如何返回numpy数组的索引? - How can I return the index of a numpy array? 如何从 Python 中的多维数组中删除元素? - How can I delete an element from a multidimensional array in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM