john22
john22

Reputation: 395

how to do padding a nested list

I have a nested list that contains 1002 time steps and in each time step, I have observation of 11 features. I have read docs related to padding but I really could not find out how to add zero elements at the end of each list. I found out the highest length of lists is for example the 24th item in my main list and now I want to pad all the rest elements based on this unless the 24th element that already in shape.As an example:

a = [[1,2,3,4,5,6,76,7],[2,2,3,4,2,5,5,5,,7,8,9,33,677,8,8,9,9],[2,3,46,7,8,9,],[3,3,3,5],[2,2],[1,1],[2,2]]

a[1] = padding(a[1],len(a[2]) with zeros at the end of the list)

I have done below:

import numpy as np
def pad_or_truncate(some_list, target_len):
    return some_list[:target_len] + [0]*(target_len - len(some_list))


for i in range(len(Length)):
    pad_or_truncate(Length[i],len(Length[24]))
    
    print(len(Length[i]))

or

for i in range(len(Length)):
   df_train_array = np.pad(Length[i],len(Length[24]),mode='constant') 

and I got this error: Unable to coerce to Series, length must be 11: given 375

Upvotes: 2

Views: 1205

Answers (3)

Nicolas Gervais
Nicolas Gervais

Reputation: 36604

You can use this simple line, which uses np.pad

list(map(lambda x: np.pad(x, (max(map(len, a)) - len(x), 0)).tolist(), a))
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 76, 7],
 [2, 2, 3, 4, 2, 5, 5, 5, 7, 8, 9, 33, 677, 8, 8, 9, 9],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 46, 7, 8, 9],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 5],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2]]

Use this if you want to pad at the end instead:

list(map(lambda x: np.pad(x, (0, max(map(len, a)) - len(x))).tolist(), a))

Upvotes: -1

Sebastien D
Sebastien D

Reputation: 4482

Solution 1

# set the max number of 0
max_len = max([len(x) for x in a])

# add zeros to the lists
temp = [x+ [0]*max_len for x in a]

#Limit the output to the wished length
[x[0:max_len] for x in temp]

Solution 2 using pandas

import pandas as pd
df = pd.DataFrame(a)
df.fillna(0).astype(int).values.tolist()

Output

[[1, 2, 3, 4, 5, 6, 76, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [2, 2, 3, 4, 2, 5, 5, 5, 7, 8, 9, 33, 677, 8, 8, 9, 9],
 [2, 3, 46, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [3, 3, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 ...]

Upvotes: 3

Nitul
Nitul

Reputation: 379

The following code snippet should pad the individual lists with the appropriate number of 0s(driven by the size of the list with the maximum elements)

def main():
    data = [
        [1,2,3,4,5,6,76,7],
        [2,2,3,4,2,5,5,5,7,8,9,33,677,8,8,9,9],
        [2,3,46,7,8,9,],
        [3,3,3,5],
        [2,2],
        [1,1],
        [2,2]
    ]

    # find the list with the maximum elements
    max_length = max(map(len, data))

    for element in data:
        for _ in range(len(element), max_length):
            element.append(0)


if __name__ == '__main__':
    main()

Upvotes: 1

Related Questions