相关疑难解决方法(0)

当数组具有不同的长度字符串时,记录数组上的numpy.concatenate失败

当尝试连接具有dtype字符串但具有不同长度的字段的记录数组时,连接失败.

正如您在下面的示例中所看到的,如果'f1'具有相同的长度但是失败则连接起作用,否则失败.

In [1]: import numpy as np

In [2]: a = np.core.records.fromarrays( ([1,2], ["one","two"]) )

In [3]: b = np.core.records.fromarrays( ([3,4,5], ["three","four","three"]) )

In [4]: c = np.core.records.fromarrays( ([6], ["six"]) )

In [5]: np.concatenate( (a,c) )
Out[5]: 
array([(1, 'one'), (2, 'two'), (6, 'six')], 
      dtype=[('f0', '<i8'), ('f1', '|S3')])

In [6]: np.concatenate( (a,b) )
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/u/jegannas/<ipython console> in <module>()

TypeError: expected a readable buffer object
Run Code Online (Sandbox Code Playgroud)

但是,如果我们只是连接数组(而不是记录),它会成功,尽管字符串的大小不同.

In [8]: np.concatenate( (a['f1'], b['f1']) )
Out[8]: 
array(['one', 'two', …
Run Code Online (Sandbox Code Playgroud)

python numpy

2
推荐指数
1
解决办法
3556
查看次数

标签 统计

numpy ×1

python ×1