0

Say I have a numpy array of vectors like this:

a = [[1,2,3], [1,2,3,4,5], [2,3]..]

What is the fastest way to pad each vector in the list with 0s so that each vector has the same length?

user2399453
  • 2,930
  • 5
  • 33
  • 60
  • I would recommend the pandas approach, if you have it. – cs95 Aug 07 '17 at 05:35
  • Find the longest list and add the missing zeros to each sublist. By the way, you do not have an array of vectors. You have a list of lists. – DYZ Aug 07 '17 at 05:35
  • The "duplicate" solution is order of magnitude slower than a core python-based solution. Pandas/numpy is not a panacea. Sometimes lists are just lists. – DYZ Aug 07 '17 at 05:45
  • @DYZ The question is tagged numpy... – cs95 Aug 07 '17 at 05:53
  • @cᴏʟᴅsᴘᴇᴇᴅ But the input is a list of lists. W/e. The OP does not seem to care about proper use of terminology. – DYZ Aug 07 '17 at 05:55
  • 1
    @DYZ If OP confirms they have a list of lists and do _not_ want a numpy solution, I will reopen this. Otherwise, it seems pointless. – cs95 Aug 07 '17 at 05:56
  • A non-numpy oneliner is: `list(zip(*(itertools.zip_longest(*a,fillvalue=0))))` (py3) – hpaulj Aug 07 '17 at 16:08

0 Answers0