【问题标题】:appending lists of list with column values from an array使用数组中的列值附加列表列表
【发布时间】:2022-01-20 09:45:57
【问题描述】:

我在一个列表中有 3 个 3x1 的数组,我想将它们附加到一个列表列表中,其中每个列表是每个数组中 1x3 的单行。

例如,转换这个:

[array([['0.6913'],
       ['0.7279'],
       ['0.724']], dtype=object), 
array([['0.6943'],
       ['0.7206'],
       ['0.714']], dtype=object), 
array([['0.6456'],
       ['0.7447'],
       ['0.7378']],dtype=object)]

到:

   [[0.6913, 0.6943, 0.6456],
    [0.7279, 0.7206, 0.7447],
    [0.724, 0.714, 0.7378]]

我该怎么做?提前谢谢你!

【问题讨论】:

    标签: python arrays


    【解决方案1】:

    使用numpy.hstack:

    import numpy as np
    
    a = [
        np.array([['0.6913'], ['0.7279'], ['0.724']], dtype=object),
        np.array([['0.6943'], ['0.7206'], ['0.714']], dtype=object),
        np.array([['0.6456'], ['0.7447'], ['0.7378']], dtype=object),
    ]
    
    np.hstack(a)
    
    array([['0.6913', '0.6943', '0.6456'],
           ['0.7279', '0.7206', '0.7447'],
           ['0.724', '0.714', '0.7378']], dtype=object)
    

    要转换为float,请使用.astype

    np.hstack(a).astype(float)
    
    array([[0.6913, 0.6943, 0.6456],
           [0.7279, 0.7206, 0.7447],
           [0.724 , 0.714 , 0.7378]])
    

    【讨论】:

    • 谢谢!我今天学到了新东西!
    • 不客气,很高兴我能帮上忙。
    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 2022-01-17
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2016-08-03
    • 2019-03-29
    相关资源
    最近更新 更多