【问题标题】:Numpy.hstack() adds the end of line mark to the result arrayNumpy.hstack() 将行尾标记添加到结果数组
【发布时间】:2016-09-03 04:09:35
【问题描述】:

我对 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'。我该如何解决这个问题?

【问题讨论】:

    标签: python numpy pyopengl


    【解决方案1】:

    数组本身没有\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'
    

    【讨论】:

    • 感谢您的解决方案,这不是错误,调试器只是按照您所说的那样显示此向量的表示
    【解决方案2】:

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

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

    【讨论】:

    • v = numpy.array(mesh.vertices, 'f') n = numpy.array(mesh.normals, 'f') t = numpy.array(mesh.texturecoords, 'f')我在构造数组时使用它
    • 我会检查 v,n 和 t 的类型以确保它们是浮点数,但我认为 Robert Kern 有以下解决方案。
    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 2022-10-25
    • 2010-12-26
    • 1970-01-01
    • 2018-06-28
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多