concatenate multiple numpy arrays in one array?

后端 未结 1 2036
轻奢々
轻奢々 2020-12-15 12:30

Assume I have many numpy array:

a = ([1,2,3,4,5])
b = ([2,3,4,5,6])
c = ([3,4,5,6,7])

and I want to generate a new 2-D array:



        
相关标签:
1条回答
  • 2020-12-15 12:42

    As mentioned in the comments you could just use the np.array function:

    >>> import numpy as np
    >>> a = ([1,2,3,4,5])
    >>> b = ([2,3,4,5,6])
    >>> c = ([3,4,5,6,7])
    
    >>> np.array([a, b, c])
    array([[1, 2, 3, 4, 5],
           [2, 3, 4, 5, 6],
           [3, 4, 5, 6, 7]])
    

    In the general case that you want to stack based on a "not-yet-existing" dimension, you can also use np.stack:

    >>> np.stack([a, b, c], axis=0)
    array([[1, 2, 3, 4, 5],
           [2, 3, 4, 5, 6],
           [3, 4, 5, 6, 7]])
    
    >>> np.stack([a, b, c], axis=1)  # not what you want, this is only to show what is possible
    array([[1, 2, 3],
           [2, 3, 4],
           [3, 4, 5],
           [4, 5, 6],
           [5, 6, 7]])
    
    0 讨论(0)
提交回复
热议问题