从 numpy 数组中删除 None 的有效方法 [英] efficient way of removing None's from numpy array

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

问题描述

是否有一种有效的方法可以从 numpy 数组中删除 Nones 并将数组大小调整为新的大小?

Is there an efficient way to remove Nones from numpy arrays and resize the array to its new size?

例如,您将如何从该帧中删除 None 而无需在 Python 中对其进行迭代.我可以轻松地遍历它,但正在处理可能被多次调用的 api 调用.

For example, how would you remove the None from this frame without iterating through it in python. I can easily iterate through it but was working on an api call that would be potentially called many times.

a = np.array([1,45,23,23,1234,3432,-1232,-34,233,None])

推荐答案

In [17]: a[a != np.array(None)]
Out[17]: array([1, 45, 23, 23, 1234, 3432, -1232, -34, 233], dtype=object)

上述工作是因为 a != np.array(None) 是一个布尔数组,它映射出非 None 值:

The above works because a != np.array(None) is a boolean array which maps out non-None values:

In [20]: a != np.array(None)
Out[20]: array([ True,  True,  True,  True,  True,  True,  True,  True,  True, False], dtype=bool)

以这种方式选择数组的元素称为 布尔数组索引.

Selecting elements of an array in this manner is called boolean array indexing.

这篇关于从 numpy 数组中删除 None 的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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