Numpy.hstack() 将行尾标记添加到结果数组

Numpy.hstack() adds the end of line mark to the result array

我对 numpy.hstack() 函数有疑问。我有三个相同的 numpy 数组,我想使用 hstack() 加入它们,所以我从这些 numpy 数组创建元组并使用 numpy.hstack(tuple)

v, n, t // rows example [ 0.83468097  0.50044298  0.229835  ]

tuple_stack = (v, n, t)

stack = numpy.hstack(tuple_stack)

结果我得到了 ndarray,其中的行看起来像这一行

[ 0.091698 0.69801199 0.88459301 0.83468097 0.50044298 0.229835\n 0.429932 0.989021 0.]

因为我用这个堆栈在 opengl 中初始化 VBO 我可能在这个对象中有错误,在第六个元素之后有'\n'。我该如何解决这个问题?

看起来您正在使用字符串数组而不是数字。您可以将 numpy 字符串数组转换为浮点数:

a = numpy.array(['0.4', '1.2\n', '.6'])
x = a.astype(numpy.float) 

数组本身没有\n。看起来你只是出于某种原因在看 repr(str(stack))

[~]
|14> stack
array([ 0.091698  ,  0.69801199,  0.88459301,  0.83468097,  0.50044298,  0.229835  ,  0.429932  ,  0.989021  ,  0.        ])

[~]
|15> print stack
[ 0.091698    0.69801199  0.88459301  0.83468097  0.50044298  0.229835
  0.429932    0.989021    0.        ]

[~]
|16> print str(stack)
[ 0.091698    0.69801199  0.88459301  0.83468097  0.50044298  0.229835
  0.429932    0.989021    0.        ]

[~]
|17> print repr(str(stack))
'[ 0.091698    0.69801199  0.88459301  0.83468097  0.50044298  0.229835\n  0.429932    0.989021    0.        ]'

[~]
|18> repr(stack[5])
'0.22983500000000001'