0

I have the following code. I want to append numpy ndarrays to an empty array. But I am not getting the required output. How can I get the output I want?

a = np.array([1,2,3])
b = np.array([2,3,4,5])
c = np.array([1,2])
p = np.array([])
p = np.append(p, a)
p = np.append(p, b)
p = np.append(p, c)

p.shape
# actual output: (9,)
# output I want: (3,?)
p[0]
# output: 1.0
# output I want: [1,2,3]
Mean Coder
  • 304
  • 1
  • 12
  • I think you're looking for something like `vstack`, but you'll have to pad your values so they're all the same width. – Anger Density Aug 20 '19 at 15:16
  • This discussion [How to make a multidimension numpy array with a varying row size?](https://stackoverflow.com/questions/3386259/how-to-make-a-multidimension-numpy-array-with-a-varying-row-size) might help you. – Alexandre B. Aug 20 '19 at 15:23
  • @AngerDensity thanks for your response. vstack is almost what I want. but it does not work for some shapes `(1, 7, 7, 60) (1, 7, 7, 60) worked (2, 7, 7, 60) (1, 7, 7, 60) worked (3, 7, 7, 60) (1, 7, 7, 35) error` – Mean Coder Aug 20 '19 at 18:10

1 Answers1

0
In [21]: a = np.array([1,2,3]) 
    ...: b = np.array([2,3,4,5]) 
    ...: c = np.array([1,2])                                                                                 
In [22]: alist = []                                                                                          
In [23]: for i in [a,b,c]: 
    ...:     alist.append(i) 
    ...:                                                                                                     
In [24]: alist                                                                                               
Out[24]: [array([1, 2, 3]), array([2, 3, 4, 5]), array([1, 2])]
In [25]: len(alist)                                                                                          
Out[25]: 3
In [26]: alist[0]                                                                                            
Out[26]: array([1, 2, 3])

You could np.array(alist) but that only creates an object dtype array with shape (3,). Shape (3,?) is meaningless in numpy.

hpaulj
  • 221,503
  • 14
  • 230
  • 353