如果条目本身是结构化数组,如何将 numpy 数组中的条目设置为 0 或 None?

我有一个结构化数组的对象列表,例如:


a = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])

b = np.array([('Dog3', 9, 81.0), ('Dog4', 3, 27.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])

c = np.array([('Dog5', 9, 81.0), ('Dog6', 3, 27.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])


lst = [a, b, c]

现在我需要这个列表本身是一个 numpy 数组,因为我需要在它上面使用numpy.where()它,否则这不起作用。


lst = np.array(lst)

那么我做这样的事情:


ID = np.where(lst == c)

lst[ID] = 0 or rather lst[ID] = None

但不是我想得到的,即


>>>lst

array([a, b, 0/None], dtype=...)

我要么得到这个:


>>>lst

array([a, b, [('0', 0,  0.), ('0', 0,  0.)]], dtype=...)

或者它根本不起作用:


TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

我怎样才能做到这一点?我必须转换lst回列表拳头吗?


慕村9548890
浏览 226回答 1
1回答

皈依舞

让我们创建一个更简单的数组,并尝试设置一些值:In [14]: dt = np.dtype([('foo','U10'),('bar',int)])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [16]: arr = np.zeros(3, dtype=dt)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [17]: arr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[17]: array([('', 0), ('', 0), ('', 0)], dtype=[('foo', '<U10'), ('bar', '<i8')])In [18]: arr[1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[18]: ('', 0)In [19]: arr[1] = 12&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [20]: arr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[20]:&nbsp;array([('',&nbsp; 0), ('12', 12), ('',&nbsp; 0)],&nbsp; &nbsp; &nbsp; dtype=[('foo', '<U10'), ('bar', '<i8')])&nbsp;&nbsp;注意字符串 '12' 和整数 12 的混合。设置一个元组,每个字段一个值:In [21]: arr[2] = ('dog',23)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;设置None失败,因为它不能转换None为整数,如第二个字段所要求的那样。In [22]: arr[0] = None&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;---------------------------------------------------------------------------TypeError&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Traceback (most recent call last)<ipython-input-22-2f7a4c897706> in <module>----> 1 arr[0] = NoneTypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'In [23]: arr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[23]:&nbsp;array([('None',&nbsp; 0), ('12', 12), ('dog', 23)],&nbsp; &nbsp; &nbsp; dtype=[('foo', '<U10'), ('bar', '<i8')])实际上它确实设法设置了字符串字段。arr是一个数组,其中每个元素必须具有相同的dtype.具有 dtype 的数组的object行为更像一个列表。虽然偶尔有用,但它不应该用作列表的替代品。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python