v9.1.1
Menu

Sequence Padder

easyFREESequence ProcessingNLP 2: Applications

Given a list of integer sequences, pad all sequences to the length of the longest sequence by appending zeros at the end.

Input: seqs, a list of integer lists (e.g. [[1, 2, 3], [4, 5], [6]]).

Output: A list of padded integer lists, one per input sequence, in the original order. The example below shows each returned sequence printed on its own line.

Example

Input
3
1 2 3
4 5
6
Output
[1, 2, 3]
[4, 5, 0]
[6, 0, 0]
Reasoning
  • Input: seqs holds three integer sequences of different lengths: [1, 2, 3] (length 3), (4,5)(4, 5) (length 2) and (6)(6) (length 1). In NLP these are typically token-ID sequences that must share one length before they can be stacked into a batch.
  • Target length: the longest sequence sets it, L=max⁡(3,2,1)=3L = \max(3, 2, 1) = 3.
  • Zeros needed: a sequence of length ℓ\ell gets L−ℓL - \ell zeros appended after its last element: 3−3=03 - 3 = 0, 3−2=13 - 2 = 1 and 3−1=23 - 1 = 2.
  • Output: three sequences, all of length 3: [1, 2, 3] is unchanged, [4, 5, 0] gains one zero, [6, 0, 0] gains two. Together they form a 3×33 \times 3 grid.
  • Notice that the original values stay at the front in their original order, and every zero sits at the end (post-padding). If all inputs already share a length (say two pairs of numbers), nothing is added.

Constraints

  • 1 ≤ n ≤ 10
  • Sequences can have different lengths
  • Pad with 0 at the end (post-padding)
  • Output each sequence as a Python list

More from NLP 2: Applications

solution.py

Test results

Sign in to run your code against the test cases.