Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用特定的数据类型填充现有numpy数组_Python_Arrays_Numpy - Fatal编程技术网

Python 如何使用特定的数据类型填充现有numpy数组

Python 如何使用特定的数据类型填充现有numpy数组,python,arrays,numpy,Python,Arrays,Numpy,假设我有一个带有固定数据类型的初始numpy数组: array = numpy.array([(1, 'a'), (2, 'b')], numpy.dtype([('idfield',numpy.int32), ('textfield', '|S256')])) 现在我需要在for循环中填充这个数组,所以我要这样做 for val in value: array = np.appe

假设我有一个带有固定数据类型的初始numpy数组:

array = numpy.array([(1, 'a'), (2, 'b')],
                    numpy.dtype([('idfield',numpy.int32),
                                 ('textfield', '|S256')]))
现在我需要在for循环中填充这个数组,所以我要这样做

for val in value:
    array = np.append(array, np.array([(val[0],val[1])],numpy.dtype([('idfield',numpy.int32),
                                                                     ('textfield', '|S256')])),axis=0)
它能工作,但看起来真的不太好!我需要在for循环中重新指定数据类型,即使在逻辑上我将使用相同的数据类型来填充数组


您知道实现此操作的更简单的方法吗?

如@juanpa.arrivillaga评论道,只定义一次数据类型更简洁:

array_dt = np.dtype([
    ('idfield', np.int32),
    ('textfield', '|S256')
])
然后将第二个值列表定义为数组,然后连接

array2 = np.array(value, array_dt)                                     
array = np.concatenate([array, array2])

np.append
np.concatenate

def append(arr, values, axis=None):
    arr = asanyarray(arr)
    if axis is None:
        if arr.ndim != 1:
            arr = arr.ravel()
        values = ravel(values)
        axis = arr.ndim-1
    return concatenate((arr, values), axis=axis)

In [89]: dt = np.dtype('U5,int')
In [90]: arr = np.array([('one',1)], dtype=dt)
In [91]: np.append(arr, ('two',2))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-91-bc17d9ad4a77> in <module>()
----> 1 np.append(arr, ('two',2))
 ...
-> 5166     return concatenate((arr, values), axis=axis)

TypeError: invalid type promotion
我不喜欢追加,因为新用户经常滥用它。通常他们认为它是一个列表附加克隆,而事实并非如此

但它确实有一个优势——它提升了0d输入的维度:

In [94]: np.concatenate([arr, np.array(('two',2),dt)])
...
ValueError: all the input arrays must have same number of dimensions
使第二个阵列1d工作:

In [95]: np.concatenate([arr, np.array([('two',2)],dt)])
Out[95]: array([('one', 1), ('two', 2)], dtype=[('f0', '<U5'), ('f1', '<i4')])
但是直接从元组列表生成数组更好:

In [101]: np.array(alist, dt)
Out[101]: 
array([('one', 1), ('two', 2), ('three', 3)],
      dtype=[('f0', '<U5'), ('f1', '<i4')])
[101]中的
:np.数组(alist,dt)
出[101]:
数组([('1',1),('2',2),('3',3)],

数据类型=[('f0',为什么不将
dtype
保存到变量中,而不是每次都重新创建它?为什么要在for循环中逐个元素地执行此操作?您的第一条语句是创建多元素结构化数组的正确方法-使用元组列表。但是如果必须使用concatenate,则所有输入都必须是数组是正确的
dtype
。它不能与数组和元组混合使用。哦,还要避免
np.append
。这是邪恶的!@MadPhysician我有使用matlab的习惯,我们可以在for循环中动态分配数组,即使数组的大小没有指定。所以我想我应该在使用pyth时改变我的习惯打开。@obchardon。你也应该改变你的MATLAB习惯。你所做的通常都是不好的实践。MATLAB linter每次都会对此抱怨。你在哪里看到一个未指定大小的数组?好吧,我还是python世界的新手,我的想法不像python专家:p。如果最好的选择是直接从元组列表我将选择此选项。谢谢
In [95]: np.concatenate([arr, np.array([('two',2)],dt)])
Out[95]: array([('one', 1), ('two', 2)], dtype=[('f0', '<U5'), ('f1', '<i4')])
In [96]: alist = [('one',1),('two',2),('three',3)]
In [97]: ll = [np.array([x],dt) for x in alist]
In [98]: ll
Out[98]: 
[array([('one', 1)], dtype=[('f0', '<U5'), ('f1', '<i4')]),
 array([('two', 2)], dtype=[('f0', '<U5'), ('f1', '<i4')]),
 array([('three', 3)], dtype=[('f0', '<U5'), ('f1', '<i4')])]

In [100]: np.concatenate(ll)
Out[100]: 
array([('one', 1), ('two', 2), ('three', 3)],
      dtype=[('f0', '<U5'), ('f1', '<i4')])
In [101]: np.array(alist, dt)
Out[101]: 
array([('one', 1), ('two', 2), ('three', 3)],
      dtype=[('f0', '<U5'), ('f1', '<i4')])