Array of arrays (Python/NumPy)

前端 未结 4 1497
北荒
北荒 2021-01-01 12:56

I am using Python/NumPy, and I have two arrays like the following:

array1 = [1 2 3]
array2 = [4 5 6]

And I would like to create a new array

4条回答
  •  悲&欢浪女
    2021-01-01 13:26

    You'll have problems creating lists without commas. It shouldn't be too hard to transform your data so that it uses commas as separating character.

    Once you have commas in there, it's a relatively simple list creation operations:

    array1 = [1,2,3]
    array2 = [4,5,6]
    
    array3 = [array1, array2]
    
    array4 = [7,8,9]
    array5 = [10,11,12]
    
    array3 = [array3, [array4, array5]]
    

    When testing we get:

    print(array3)
    
    [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]
    

    And if we test with indexing it works correctly reading the matrix as made up of 2 rows and 2 columns:

    array3[0][1]
    [4, 5, 6]
    
    array3[1][1]
    [10, 11, 12]
    

    Hope that helps.

提交回复
热议问题