0

I have a Numpy array of arrays and each array inside the main array has a different size. I need to pad the end with 0s at each array so that all arrays that are inside the main array are of equal length but I do not know the max length. I want it to be done in a cleaner way instead of finding the max length and assigning 0's at the end as and when required. a below is already a Numpy aray

a=[
   [1,2,3,4],
   [3,56],
   [8,4,8,4,9,33,55]
  ] .
   In this case maxlength is 7(the length of third array) . 
   I want final array to look like follows 
a=[
   [1,2,3,4,0,0,0],
   [3,56,0,0,0,0,0],
   [8,4,8,4,9,33,55]
  ]
swifty
  • 165
  • 1
  • 1
  • 17

2 Answers2

0

For numpy.pad solution I think we need to ensure your input is exactly as you have it so we can get a proper solution. Then it will just be:

a=[
      np.asarray([1,2,3,4]),
      np.asarray([3,56]),
      np.asarray([8,4,8,4,9,33,55])
 ]


max_len = max([len(x) for x in a])

output = [np.pad(x, (0, max_len - len(x)), 'constant') for x in a]

print(output)

>>> [
     array([1, 2, 3, 4, 0, 0, 0]), 
     array([ 3, 56,  0,  0,  0,  0,  0]), 
     array([ 8,  4,  8,  4,  9, 33, 55])
    ]
Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
0

Quick and dirty:

a=[
   [1,2,3,4],
   [3,56],
   [8,4,8,4,9,33,55]
  ]

n = len(a)
m = max([len(x) for x in a])

A = np.zeros((n,m))
for i in range(n):
    A[i,:len(a[i])] = a[i]
print A

As I saw in your comment on @Scott Skiles solution, that you want to use np.pad for this, wou can change the line within the for loop by:

A[i] = np.pad(a[i],(0,m-len(a[i])),'constant', constant_values=0)
Glostas
  • 1,090
  • 2
  • 11
  • 21
  • can i use np.pad and automatically do it instead of m=max([len(x) for x in a]) and for loop? – swifty Mar 20 '19 at 12:33
  • I donot think so, as np.pad needs a numpy array (or alike) as input. Your input is not alike. If you need it more often, just make a function – Glostas Mar 20 '19 at 12:39
  • it works , but I see that in the process a becomes list of list at the end – swifty Mar 20 '19 at 12:53
  • print type(A) gives for me. I am not sure if I understood your expected output correctly. I thought you wanted a numpy array in the end. If you want a list of np.arrays just stick to Scott's solution – Glostas Mar 20 '19 at 13:04