1

I have an array with unknown length (for example lets use 11). so the array is

[1,2,3,4,5,6,7,8,9,10,11]

I want to reshape that array so he will have 5 columns and as much rows as needed. I know I can use reshape(-1,5) that way it creates the rows based on the array length.

But it gives me this error:

ValueError: cannot reshape array of size 11 into shape (5)

Any idea how do I do that? the desired result is:

[[1,2,3,4,5],
[6,7,8,9,10],
[11,None,None,None,None]]

The code that I ran and got this error is:

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9,10,11])    
print(np.reshape(a, (-1,5)))
orr
  • 129
  • 10
  • Dup of https://stackoverflow.com/questions/41253044/can-i-use-np-resize-to-pad-an-array-with-np-nan ? – crayxt Aug 25 '19 at 10:42

2 Answers2

1

You can do it without numpy.

ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
n = 5
reshaped = [ar[i: i + n] + [None] * (i + n - len(ar)) for i in range(0, len(ar), n)]

You can also use trick with iterator (chunks will be in tuples):

reshaped = list(zip(*[iter(ar + [None] * (n - len(ar) % n))] * n))

You can apply zip_longest() from itertools to not add None values by yourself:

from itertools import zip_longest
reshaped = list(zip_longest(*[iter(ar)] * n))
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
  • Is there a more efficient way? maybe one numpy function or withput loops? – orr Aug 25 '19 at 10:43
  • @orr. what do you mean under "efficient"? You can't split list to chunks without loops. I'm not specialist in numpy, so I don't know it's methods. – Olvin Roght Aug 25 '19 at 10:49
  • by efficient I mean built-in numpy functions so that will make faster runtime. your answer is great, but I am curious to knoe whether there is faster way. – orr Aug 25 '19 at 19:31
  • @orr, if tests from [this](https://stackoverflow.com/a/50555709/10824407) answer is correct, numpy 3 times slower then list comprehension. – Olvin Roght Aug 25 '19 at 19:47
  • haha thats funny, I thought numpy should be more faster. I guess I was wrong, thanks for the enlightenment. – orr Aug 28 '19 at 19:05
0
In [135]: res = np.empty((3,5), object)                                                                      
In [136]: res                                                                                                
Out[136]: 
array([[None, None, None, None, None],
       [None, None, None, None, None],
       [None, None, None, None, None]], dtype=object)
In [137]: res.flat[:11] = np.arange(1,12)                                                                    
In [138]: res                                                                                                
Out[138]: 
array([[1, 2, 3, 4, 5],
       [6, 7, 8, 9, 10],
       [11, None, None, None, None]], dtype=object)
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • the thing is I dont know the row number, so I dont know the size of the empty None array I need. I only know the columns – orr Aug 25 '19 at 19:29
  • But you can calculate the row number. The next multiple of 5 up from 11 is 15. 15/5 gives me 3. – hpaulj Aug 25 '19 at 19:45