【问题标题】:How to combine two 2D arrays in python?如何在python中组合两个二维数组?
【发布时间】:2020-04-05 23:35:59
【问题描述】:

如图所示如何组合两个二维数组?

sample_list = [np.array([1, 2, 3]), np.array([4, 5, 6)])]
sample_objects = [np.array([object_1, object_2, object_3]), np.array([object_4, object_5, object_6])]

result = [np.array([(1, object_1), (2, object_2), (3, object_3)]), np.array([(4, object_4), (5, object_5), (6, object_)])

【问题讨论】:

  • 为什么要混合整数和object_n?坚持使用列表不是更简单吗?

标签: arrays python-3.x list numpy


【解决方案1】:

使用zip尝试以下操作:

result = [np.array(list(zip(sample_list[i], sample_objects[i]))) for i in range(len(sample_list))]

【讨论】:

    【解决方案2】:

    您的问题的纯列表版本:

    In [121]: sample_list = [[1, 2, 3], [4, 5, 6]] 
         ...: sample_objects = [["object_1", "object_2", "object_3"], ["object_4", "object_5", "object_6"]]                                                                             
    In [122]: sample_list                                                                          
    Out[122]: [[1, 2, 3], [4, 5, 6]]
    In [123]: sample_objects                                                                       
    Out[123]: [['object_1', 'object_2', 'object_3'], ['object_4', 'object_5', 'object_6']]
    In [124]: [list(zip(a,b)) for a,b in zip(sample_list, sample_objects)]                         
    Out[124]: 
    [[(1, 'object_1'), (2, 'object_2'), (3, 'object_3')],
     [(4, 'object_4'), (5, 'object_5'), (6, 'object_6')]]
    

    将东西包装在np.array 中几乎不会增加问题。请注意,我必须用字符串替换您的 objects

    sample_objects,如果它确实包含“对象”同时是object dtype:

    sample_objects = [np.array(["object_1", "object_2", "object_3"],object), 
        np.array(["object_4", "object_5", "object_6"],object)]  
    

    ====

    定义一个示例类

    class MyObj:
        def __init__(self,n):
            self.n = n
        def __repr__(self):
            return f'object_{self.n}'
    

    还有你的数组:

    In [141]: sample_list = [np.array([1, 2, 3]), np.array([4, 5, 6])] 
         ...: sample_objects = [np.array([MyObj(n) for n in [1,2,3]]), np.array([MyObj(n) for n in 
         ...: [4,5,6]])]                                                                                      
    In [142]: sample_list                                                                          
    Out[142]: [array([1, 2, 3]), array([4, 5, 6])]
    In [143]: sample_objects                                                                       
    Out[143]: 
    [array([object_1, object_2, object_3], dtype=object),
     array([object_4, object_5, object_6], dtype=object)]
    

    以及组合它们的list zip方式:

    In [153]: [np.array(list(zip(a,b))) for a,b in zip(sample_list, sample_objects)]               
    Out[153]: 
    [array([[1, object_1],
            [2, object_2],
            [3, object_3]], dtype=object), array([[4, object_4],
            [5, object_5],
            [6, object_6]], dtype=object)]
    

    以及使用np.stack的变体:

    In [154]: [np.stack((a,b), axis=1) for a,b in zip(sample_list, sample_objects)]                
    Out[154]: 
    [array([[1, object_1],
            [2, object_2],
            [3, object_3]], dtype=object), array([[4, object_4],
            [5, object_5],
            [6, object_6]], dtype=object)]
    

    hstack 不会将它们交错:

    array([1, 2, 3, object_1, object_2, object_3], dtype=object)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 2017-12-13
      • 2012-08-14
      • 1970-01-01
      • 2014-09-14
      相关资源
      最近更新 更多