一次删除numpy数组中某个值的元素 [英] Delete an element of certain value in numpy array once

查看:577
本文介绍了一次删除numpy数组中某个值的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从numpy数组中删除具有特定值的元素。但是,在有多个具有相同值的元素的情况下,我只想删除一个实例(与哪个实例无关)。那就是: ,(8、8、8、9]))

如何删除8个实例?

  a_new = np.delete(a,np.where(a == 8))
print(a_new )

删除所有8位。

解决方案

您只需选择一个索引即可:

  In [3]:np.delete(a,np.where(a == 8)[0] [0])
Out [3]:array([1,1 ,2,6,8,8,9])


I would like to delete an element from a numpy array that has a certain value. However, in the case there are multiple elements of the same value, I only want to delete one occurrence (doesn't matter which one). That is:

import numpy as np
a = np.array([1, 1, 2, 6, 8, 8, 8, 9])

How do I delete one instance of 8? Specifically

a_new = np.delete(a, np.where(a == 8))
print(a_new)

removes all 8's.

解决方案

You can simply choose one of the indices:

In [3]: np.delete(a, np.where(a == 8)[0][0])
Out[3]: array([1, 1, 2, 6, 8, 8, 9])

这篇关于一次删除numpy数组中某个值的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆