Skip to content

Instantly share code, notes, and snippets.

@hotbaby
hotbaby / list2tensor.py
Last active Apr 13, 2021
Pytorch List转Tensor
View list2tensor.py
l = [[1, 1675, 4028, 8271, 42, 2], [1, 1675, 4028, 8844, 10135, 10757, 9478, 42, 2]]
# maximum rows and columns
max_rows = len(l)
max_cols = max(len(row) for row in l)
# padded rows
padded = [row + [0] * (max_cols - len(row)) for row in l]
# unpack nested list